迭代仿真存在严重的性能问题

时间:2018-07-12 20:59:55

标签: matlab performance simulation

我最近在实现模拟算法时偶然发现了性能问题。我设法找到了瓶颈函数(信号是arrayfun的内部调用使一切变慢了):

function sim = simulate_frequency(the_f,k,n)

    r = rand(1,n); % 
    x = arrayfun(@(x) find(x <= the_f,1,'first'),r);
    sim = (histcounts(x,[1:k Inf]) ./ n).';

end

它正在代码的其他部分中使用,如下所示:

h0 = zeros(1,sims);

for i = 1:sims
    p = simulate_frequency(the_f,k,n);
    h0(i) = max(abs(p - the_p));
end

以下是一些可能的值:

% Test Case 1
sims = 10000;
the_f = [0.3010; 0.4771; 0.6021; 0.6990; 0.7782; 0.8451; 0.9031; 0.9542; 1.0000];
k = 9;
n = 95;

% Test Case 2
sims = 10000;
the_f = [0.0413; 0.0791; 0.1139; 0.1461; 0.1760; 0.2041; 0.2304; 0.2552; 0.2787; 0.3010; 0.3222; 0.3424; 0.3617; 0.3802; 0.3979; 0.4149; 0.4313; 0.4471; 0.4623; 0.4771; 0.4913; 0.5051; 0.5185; 0.5314; 0.5440; 0.5563; 0.5682; 0.5797; 0.5910; 0.6020; 0.6127; 0.6232; 0.6334; 0.6434; 0.6532; 0.6627; 0.6720; 0.6812; 0.6901; 0.6989; 0.7075; 0.7160; 0.7242; 0.7323; 0.7403; 0.7481; 0.7558; 0.7634; 0.7708; 0.7781; 0.7853; 0.7923; 0.7993; 0.8061; 0.8129; 0.8195; 0.8260; 0.8325; 0.8388; 0.8450; 0.8512; 0.8573; 0.8633; 0.8692; 0.8750; 0.8808; 0.8864; 0.8920; 0.8976; 0.9030; 0.9084; 0.9138; 0.9190; 0.9242; 0.9294; 0.9344; 0.9395; 0.9444; 0.9493; 0.9542; 0.9590; 0.9637; 0.9684; 0.9731; 0.9777; 0.9822; 0.9867; 0.9912; 0.9956; 1.000];
k = 90;
n = 95;

标量sims必须在1000 1000000范围内。累积频率向量the_f永远不会包含超过100个元素。标量k表示the_f中元素的数量。最后,标量n表示经验样本矢量中的元素数量,甚至可以非常大(据我所知,最多可以是10000个元素)。

关于如何缩短此过程的计算时间的任何线索吗?

3 个答案:

答案 0 :(得分:6)

在我看来,在第二个测试用例中,这比第一个测试用例要快一些。对于更长的the_f和更大的n,时间差可能更大。

function sim = simulate_frequency(the_f,k,n)
    r = rand(1,n); % 
    [row,col] = find(r <= the_f); % Implicit singleton expansion going on here!
    [~,ind] = unique(col,'first');
    x = row(ind);
    sim = (histcounts(x,[1:k Inf]) ./ n).';
end

我在r <= the_f中使用隐式单例扩展,如果您使用的是较旧版本的MATLAB(但您知道演练),请使用bsxfun

查找然后将行和列返回到r大于the_f的所有位置。 unique为每一列的第一个元素找到结果的索引。

信用:Andrei Bobrov over on MATLAB Answers


另一个选项(源自this other answer)更短一些,但也使IMO更加晦涩:

mask = r <= the_f;
[x,~] = find(mask & (cumsum(mask,1)==1));

答案 1 :(得分:5)

如果我想要性能,我会避免使用arrayfun。甚至这个for循环都更快:

function sim = simulate_frequency(the_f,k,n)

    r = rand(1,n); % 
    for i = 1:numel(r)
        x(i) = find(r(i)<the_f,1,'first');
    end
    sim = (histcounts(x,[1:k Inf]) ./ n).';

end

使用第一组样本数据运行10000个模拟会给出以下计时。

您的arrayfun函数:

>Elapsed time is 2.848206 seconds.

for循环函数:

>Elapsed time is 0.938479 seconds.

受克里斯·伦戈(Cris Luengo)的回答启发,我建议如下:

function sim = simulate_frequency(the_f,k,n)

    r = rand(1,n); % 
    x = sum(r > the_f)+1;
    sim = (histcounts(x,[1:k Inf]) ./ n)';

end

时间:

>Elapsed time is 0.264146 seconds.

答案 2 :(得分:2)

您可以将histcountsr一起使用作为输入:

r = rand(1,n);
sim = (histcounts(r,[-inf ;the_f]) ./ n).';

如果使用histc代替histcounts,则可以对整个模拟进行矢量化处理:

r = rand(n,sims);
p = histc(r, [-inf; the_f],1);
p = [p(1:end-2,:) ;sum(p(end-1:end,:))]./n;
h0 = max(abs(p-the_p(:)));    %h0 = max(abs(bsxfun(@minus,p,the_p(:))));