2

我对从 matlab 结构或 matlab 变量(任何数组)访问/重新分配变量时的时间有疑问:

想象一下,您有一个创建十个变量(不同维度和大小的数组)的函数。该函数在另一个需要生成这些变量的函数中被调用。

现在,因为从函数中获取十个变量看起来很麻烦,所以我考虑将这十个变量存储在一个结构中,并更改我的初始函数,使其仅输出一个结构(有十个字段)而不是十个变量。

因为时间对我来说至关重要(它是 EEG 实验的代码),我想确保 struct 方法不慢,所以我编写了以下测试函数。

function test_timingStructs

%% define struct
strct.a=1; strct.b=2; strct.c=3;

%% define "loose" variables
a = 1; b = 2; c = 3;

%% How many runs?
runs = 1000;

%% time access to struct
x = []; % empty variable
tic
for i=1:runs
    x = strct.a; x = strct.b; x = strct.c;
end
t_struct = toc;

%% time access to "loose variables"
x = []; % empty variable
tic
for i=1:runs
    x = a; x = b; x = c;
end
t_loose = toc;

%% Plot results
close all; figure;
bar(cat(2,t_struct,t_loose));
set(gca,'xticklabel', {'struct', 'loose variable'})
xlabel('variable type accessed', 'fontsize', 12)
ylabel(sprintf('time (ms) needed for %d accesses to 3 different variables', runs), 'fontsize', 12)
title('Access timing: struct vs "loose" variables', 'fontsize', 15)

end

根据结果​​,访问结构以获取字段的值比仅访问变量要慢得多。我可以根据我所做的测试做出这个假设吗?

当我想访问它们时,是否有另一种方法可以巧妙地“打包”十个变量而不会浪费时间?

结果

4

1 回答 1

3

理论上,是的,访问 a 中的数据struct会比访问存储在变量中的数据慢。这只是更高级别数据类型产生的开销。

在您的测试中,您只测量两个数据结构中数据的访问时间。当您使用变量时,只需将一个变量分配给另一个变量会花费很少的时间,因为 MATLAB 使用写时复制,并且在修改数据之前实际上不会在内存中复制数据。

因此,您编写的测试对于确定使用 a 的实际成本并不是很有用,struct因为我确信您的函数会对它接收到的数据做一些事情。一旦您修改数据,MATLAB 将复制数据并执行请求的操作。因此,要确定 a 的性能损失是多少struct,您应该为实际函数计时,而不是您正在使用的无操作函数。

更真实的测试

我在下面编写了一个测试,它比较了struct被调用函数执行和不修改数据的和变量访问。

function timeaccess

    sz = round(linspace(1, 200, 100));

    [times1, times2, times3, times4] = deal(zeros(size(sz)));

    for k = 1:numel(sz)

        n = sz(k);

        S = struct('a', rand(n), 'b', rand(n), 'c', rand(n));
        times1(k) = timeit(@()access_struct(S));
        S = struct('a', rand(n), 'b', rand(n), 'c', rand(n));
        times2(k) = timeit(@()access_struct2(S));
        a = rand(n); b = rand(n); c = rand(n);
        times3(k) = timeit(@()access_vars(a, b, c));
        a = rand(n); b = rand(n); c = rand(n);
        times4(k) = timeit(@()access_vars2(a, b, c));
    end

    figure

    hax1 = subplot(1,2,1);
    ylabel('Execution Time (ms)')
    xlabel('Size of Variables');

    hold on

    plot(sz, times2 * 1000, 'DisplayName', 'Struct w/o modification')
    plot(sz, times4 * 1000, 'DisplayName', 'Variables w/o modification')

    legend(findall(hax1, 'type', 'line'))

    hax2 = subplot(1,2,2);
    ylabel('Execution Time (ms)')
    xlabel('Size of Variables');
    hold on

    plot(sz, times1 * 1000, 'DisplayName', 'Struct w modification')
    plot(sz, times3 * 1000, 'DisplayName', 'Variables w modification')

    legend(findall(hax2, 'type', 'line'))

    saveas(gcf, 'data_manipulation.png')
    legend()
end

function [a, b, c] = access_struct(S)
    a = S.a + 1;
    b = S.b + 2;
    c = S.c + 3;
end

function [a, b, c] = access_struct2(S)
    a = S.a;
    b = S.b;
    c = S.c;
end

function [d, e, f] = access_vars(a, b, c)
    d = a + 1;
    e = b + 1;
    f = c + 1;
end

function [d, e, f] = access_vars2(a, b, c)
    d = a;
    e = b;
    f = c;
end

结果

如您所见,struct仅将一个变量分配给另一个变量的速度较慢,但​​只要我执行一个操作(这里我有一个非常简单的操作,即为每个变量添加一个常量),访问时间的影响可以忽略不计.

在此处输入图像描述

概括

根据上面的测试,我假设两者之间的时间差对于您的用例来说可以忽略不计。即使struct它有点慢,它也可能是一个更简洁的设计,并产生更多可读/可维护的代码,并且可能值得在性能上有所不同。

如果您非常关心性能,那么可能值得研究 C/C++ mex 函数来为您完成一些繁重的工作或切换到比 MATLAB 性能更高的语言。

于 2016-12-03T15:17:20.780 回答