Cody

Problem 808. Hamming Weight - Fast

Solution 188759

Submitted on 10 Jan 2013 by Tomasz
• Size: 0
• 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 Status Code Input and Output
1   Pass
%% feval(@assignin,'caller','score',2000);

``` ```

2   Pass
%% global num_ones b1 b2 b3 b4 b5 net_time net_time=2000; % 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

``` ```

3   Pass
%% global num_ones b1 b2 b3 b4 b5 net_time w=uint32(randi(2^32,[65536*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 65536 values %.0f usec\n',dt); assert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\n',dt))

``` ones8 = 0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 4 5 5 6 5 6 6 7 5 6 6 7 6 7 7 8 Time to execute 65536 values 67 usec ```

4   Pass
%% global num_ones b1 b2 b3 b4 b5 net_time w=uint32(randi(2^32,[65536*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('Time to execute 4096*4096 values %.0f msec\n',net_time))

```Time to execute 4096*4096 values 0 msec ```

5   Pass
%% global net_time % net_time in ms % Create graph data net_time=min(2000,net_time); % Limit graph y-axis feval(@assignin,'caller','score',floor(net_time)); %fh=fopen('Ham.m','wt'); %fprintf(fh,'%s\n',repmat('1;',[1,round(net_time/2)])); %fclose(fh);

``` ```