Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 194806

Submitted on 21 Jan 2013 by Alfonso Nieto-Castanon

Correct

21Size
This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
global num_ones b1 b2 b3 b4 b5 net_time
net_time=4000; % default in case of time out (not needed)
b1=uint32(1431655765);
b2=uint32(858993459);
b3=uint32(252645135);
b4=uint32(16711935);
b5=uint32(65535);
num_ones=uint32(zeros(65536,1));
for i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ;
end % Cody 0.996 sec

                    
2
Pass
 
%%
global num_ones b1 b2 b3 b4 b5 net_time
w=uint32(randi(2^32,[4096*1,1])-1);
for i=1:4 % Clear timing
  vw=Ham(w);
end
wexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56
t0=clock;
 vw=Ham(w);
 vw=Ham(w);
 vw=Ham(w);
 vw=Ham(w);
dt=etime(clock,t0)*250*1000; % avg of 4 runs in us
fprintf('Time to execute 4096 values %.0f usec\n',dt);
assert(isequal(wexpect,vw),sprintf('\nTime to execute 4096 values %.0f usec\n',dt))
Time to execute 4096 values 460 usec
3
Pass
 
%%
global num_ones b1 b2 b3 b4 b5 net_time
w=uint32(randi(2^32,[4096*1,1])-1);
 vw=Ham(w); % Three cycles of smaller vector
 vw=Ham(w);
 vw=Ham(w);
w=uint32(randi(2^32,[4096*4096,1])-1);
wexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56
  vw=Ham(w); % Big Prep file
  vw=Ham(w); % Big Prep file
t0=clock;
 vw=Ham(w);
 vw=Ham(w);
 vw=Ham(w);
 vw=Ham(w);
net_time=etime(clock,t0)*250; % avg of 4 runs
fprintf('Time to execute 4096*4096 values %.0f msec\n',net_time);
assert(isequal(wexpect,vw),sprintf('\nTime to execute 4096*4096 values %.0f msec\n',net_time))
Time to execute 4096*4096 values 6006 msec