speeding up a function

1 次查看(过去 30 天)
Hi all, I have the following function that takes x which is a unit32 vector of about 2 billion elements. Basically it expands the decimal numbers and then classify each element based on the structure of the binary string:
function class = classX( x )
class=uint32(zeros(size(x)));
parfor i=1:length(x)
binary=de2bi(x(i),32);
if binary(1)==0 && binary(2)==0
class(i)=1;
elseif binary(1)==0 && binary(2)==1
class(i)=2;
end
end
I'm looking for a possible way to speed this function as much as possible. I look forward to your creative ideas.
AD
  2 个评论
John D'Errico
John D'Errico 2015-11-20
What is de2bi? What does it do? If I guess, then my guess will be wrong.
Stephen23
Stephen23 2015-11-20
编辑:Stephen23 2015-11-20
@John D'Errico: I assume that it is the Communications Toolbox function:

请先登录,再进行评论。

采纳的回答

Mohammad Abouali
Mohammad Abouali 2015-11-20
编辑:Mohammad Abouali 2015-11-20
Ok, somehow my post got deleted. I asked whether you have other classes or there are just these two classes.
here is a sample:
fprintf('Initiating a sample vector ...');tic
xSize=1e6;
x=randi(xSize,[xSize ,1],'single');
fprintf('- took %0.2f[s].\n',toc);
fprintf('Running a serial version of the code ...');tic;
tmpVar=de2bi(x(:));
classList=uint8([1 0 2 0])';
%binary(1), binary(2), decimal value, class
% 0 , 0 , 0 , 1
% 0 , 1 , 1 , 0
% 1 , 0 , 2 , 2
% 1 , 1 , 3 , 0
type=classList( bi2de(tmpVar(:,1:2)) +1);
fprintf('- took %0.2f[s].\n',toc);
pay attention to how classList is created. You can use this to classify them into as many classes as you want and the execution time does not change. This is usually better when dealing with integer classes relative to the case when you want to do logical indexing. Checking conditions take longer cycles.
Note that this code is in serial; On my system it takes about 0.36 seconds for 1M elements, and it takes about 3.73 for 10M elements. So if it holds the same ratio (which most possibly won't) for 2B elements it should take about 750seconds. However, this is serial. No parallel at all.

更多回答(1 个)

Stephen23
Stephen23 2015-11-20
编辑:Stephen23 2015-11-20
For a random vector of 10000 elements my code below is 30000 times faster than your code:
Elapsed time is 27.6156 seconds.
Elapsed time is 0.000999928 seconds.
And for 100000 elements it is nearly 50000 times faster:
Elapsed time is 336.889 seconds.
Elapsed time is 0.00700092 seconds.
And of course the test outputs are always exactly identical:
>> isequal(A,B)
ans = 1
This is the code:
function out = classx2(x)
out = zeros(size(x),'uint8');
out(x<pow2(30)) = 1;
out(pow2(30)<=x & x<pow2(31)) = 2;
end
  3 个评论
Stephen23
Stephen23 2015-11-20
编辑:Stephen23 2015-11-20
@Mohammad Abouali: thank you for pointing this out. I assumed it is much like dec2bin.
Mohammad Abouali
Mohammad Abouali 2015-11-20
no, it is much better. dec2bin used to return str which needed some fixing. But de2bi returns numerics.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by