索引创建的性能

标签 performance matlab

在尝试选择推荐的索引方法时,我尝试衡量性能。然而,测量结果让我很困惑。我以不同的顺序运行了多次,但测量值保持一致。 以下是我衡量性能的方法:

for N = [10000 15000 100000 150000]
    x =  round(rand(N,1)*5)-2;
    idx1 = x~=0;
    idx2 = abs(x)>0;

    tic
    for t = 1:5000
        idx1 = x~=0;
    end
    toc

    tic
    for t = 1:5000
        idx2 = abs(x)>0;
    end
    toc
end

这是结果:

Elapsed time is 0.203504 seconds.
Elapsed time is 0.230439 seconds.

Elapsed time is 0.319840 seconds.
Elapsed time is 0.352562 seconds.

Elapsed time is 2.118108 seconds. % This is the strange part
Elapsed time is 0.434818 seconds.

Elapsed time is 0.508882 seconds.
Elapsed time is 0.550144 seconds.

我检查过,对于 100000 左右的值也会发生这种情况,即使在 50000 时也会发生奇怪的测量。

所以我的问题是:有没有其他人在一定范围内经历过这种情况,是什么原因造成的? (这是一个错误吗?)

最佳答案

我认为这与 JIT 有关(以下结果使用 2011b)。根据系统、Matlab 版本、变量大小以及循环中的确切内容,使用 JIT 并不总是更快。这与“预热”效应有关,有时如果您在一个 session 中多次运行一个 m 文件,它在第一次运行后会变得更快,因为加速器只需要编译代码的某些部分一次。

JIT 开启(功能加速开启)

Elapsed time is 0.176765 seconds.
Elapsed time is 0.185301 seconds.

Elapsed time is 0.252631 seconds.
Elapsed time is 0.284415 seconds.

Elapsed time is 1.782446 seconds.
Elapsed time is 0.693508 seconds.

Elapsed time is 0.855005 seconds.
Elapsed time is 1.004955 seconds.

JIT 关闭(功能加速关闭)

Elapsed time is 0.143924 seconds.
Elapsed time is 0.184360 seconds.

Elapsed time is 0.206405 seconds.
Elapsed time is 0.306424 seconds.

Elapsed time is 1.416654 seconds.
Elapsed time is 2.718846 seconds.

Elapsed time is 2.110420 seconds.
Elapsed time is 4.027782 seconds.

预计到达时间,看看如果您使用整数而不是 double 会发生什么,这很有趣:

启用 JIT,相同的代码,但使用 int8 转换 x

Elapsed time is 0.202201 seconds.
Elapsed time is 0.192103 seconds.

Elapsed time is 0.294974 seconds.
Elapsed time is 0.296191 seconds.

Elapsed time is 2.001245 seconds.
Elapsed time is 2.038713 seconds.

Elapsed time is 0.870500 seconds.
Elapsed time is 0.898301 seconds.

关闭 JIT,使用 int8

Elapsed time is 0.198611 seconds.
Elapsed time is 0.187589 seconds.

Elapsed time is 0.282775 seconds.
Elapsed time is 0.282938 seconds.

Elapsed time is 1.837561 seconds.
Elapsed time is 1.846766 seconds.

Elapsed time is 2.746034 seconds.
Elapsed time is 2.760067 seconds.

关于索引创建的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17809077/

相关文章:

ios - 快速模糊 UITableViewCell contentView 背景

C#/F#性能比较

Java StringBuilder 参数性能

c++ - 将此 C++ 代码转换为 Matlab

matlab - 在不删除颜色条的情况下在现有轴内显示新图像

matlab - 热图中的旋转轴刻度标签

python - 无法以足够的精度将 matlab datenum 格式转换为 python datetime

performance - Actionscript 3 中循环的变量声明性能

WPF 应用程序行为取决于 Windows 用户访问级别

matlab更新时间向量