I need most efficient code for these commands

% convert the real number to integer, then get the binary for it. circulate the binary to the left four times, get a %new number . the time to execute the code is too much . I need help to execute it in less time. I mean efficient code
N=2048;
x=[0.3456365243]
r=[ 3.5671234654 ];
W=zeros(N,1);
for k = 1:N,
x(1)=r(1)*x(1)*(1-x(1));
z=mod(floor(x(1)*2^16),(2^15-1)); %this operation ensures z include[0,32767]
bin=dec2bin(z,16);
vec=bin-'0';
y=circshift(vec,[1 -4]);
keylove=y(15)*2^14 + y(14)*2^13 + y(13)*2^12 + y(12)*2^11 + y(11)*2^10 + ...
y(10)*2^9 + y(9)*2^8 + y(8)*2^7 + y(7)*2^6 + y(6)*2^5 + ...
y(5)*2^4 + y(4)*2^3 + y(3)*2^2 + y(2)*2^1 + y(1)*2^0;
if y(16)==1,
keylove=keylove*(-1);
end %%%%%%%%%%%%%% end if
W(k)=keylove;
end

 采纳的回答

dpb
dpb 2016-2-15
编辑:dpb 2016-2-15
Well, as Walter says but replacing "most" with "more", with Matlab replacing all the exponentiations with their numeric equivalents is, I think, bound to help. That is,
keylove=y(15)*16384 + y(14)*8192 + y(13)*4096 + y(12)*2048 + y(11)*1024 + ...
And, of course, you should test whether the explicit expression beats (I'd guess not) the vectorized solution of
kelove=y(1:15)*pows2; % where pows2 is precomputed pows2=pow2(0:14).';
The conversion of internal storage to the character binary representation and that manipulation is also quite slow...use bit operations instead--
z=uint16(mod(floor(x(1)*65536),65535));
y=bitor(bitshift(z,4,'uint16'),bitand(z,15));
Matlab doesn't have builtin function to do the flip of the ordering required to directly evaluate keylove, unfortunately, and nothing leapt out at me at the moment...
Addendum
Well, isn't this a kick in the head! :( bitset won't set multiple bits into a target but returns a new variable for every bit to be set. Otherwise, the following could also possibly help.
bits=find(bitget(y,1:16));
y=bitset(0,17-bits,'uint16');
Unfortunately, the last yields--
>> y=bitset(0,17-bits,'uint16')
y =
32768 8192 2048 512 128 64 32 1
>> sum(y(2:end))
ans =
10977
>>
which agrees with the answer from your original code for the same case (as expected). With a "real" bit-setting function, all bits in the argument list could be set in the one integer storage location and you'd have the answer automagically thereby eliminating that summation ("no operation can be made more efficient than the one you avoid doing needlessly").
It'd also help if the bitshift function had a wraparound mode instead of only truncation so could avoid the multiple hacks there as well. All in all, Matlab isn't a "bit-banging" tool of any reknown; nor was it intended to be. On occasion, however, it would be nice to be a little more friendly than it is.
Probably the only way to get real performance from this will be to write a mex function altho I'd think the above and a few other tweaks should help quite a bit. I didn't try timing anything here...

2 个评论

Thanks but it didn't make a significant difference on time
Well, nothing says you have to stop there...what does the profiler say and what is your current incarnation?

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by