Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance Matlab fprintf-什么更快-使用带模if编写所有输出或仅编写少数输出?_Performance_Matlab_Stdout - Fatal编程技术网

Performance Matlab fprintf-什么更快-使用带模if编写所有输出或仅编写少数输出?

Performance Matlab fprintf-什么更快-使用带模if编写所有输出或仅编写少数输出?,performance,matlab,stdout,Performance,Matlab,Stdout,在我的特殊情况下,我想为matlab确定这一点。使用“if”是否更快(在for循环中运行约250.000次),所以fprintf只使用了250次 for i=1:250042 if ~(mod(i, 1000)) fprintf(<something to standard output>); end end i=1:250042的 if~(mod(i,1000)) fprintf(); 终止 终止 我知道对于C语言编程,当打印到标准输出时,程序要慢得多。首先,我

在我的特殊情况下,我想为matlab确定这一点。使用“if”是否更快(在for循环中运行约250.000次),所以fprintf只使用了250次

for i=1:250042
  if ~(mod(i, 1000))
    fprintf(<something to standard output>);
  end
end
i=1:250042的

if~(mod(i,1000))
fprintf();
终止
终止

我知道对于C语言编程,当打印到标准输出时,程序要慢得多。

首先,我不喜欢使用
I
作为索引,因为它在Matlab中表示虚数。 您的问题很容易测试:

tic
for jj=1:250042
  if ~mod(jj,1000)
    disp('Hello')
  end
end

a=toc;

tic

for jj=1:250042
  if ~mod(jj,100)
    disp('Hello')
  end
end

b=toc;
clc
disp(a)
disp(b)
给予


所以答案是:是的,少打印会更快。

打印所有内容会慢得多。通过分析以下代码

clear all
clear classes

for i=1:250042
    if ~(mod(i, 1000))
        fprintf(['current loop: ', num2str(i), '\n']);
    end
end

for i=1:250042
    fprintf(['current loop: ', num2str(i), '\n']);
end
我发现:

   time   calls  line
< 0.01       1    1 clear all 
< 0.01       1    2 clear classes 
                  3 
             1    4 for i=1:250042 
  0.13  250042    5     if ~(mod(i, 1000)) 
  0.12     250    6         fprintf(['current loop: ', num2str(i), '\n']); 
           250    7     end 
  0.24  250042    8 end 
                  9 
             1   10 for i=1:250042 
 37.90  250042   11     fprintf(['current loop: ', num2str(i), '\n']); 
  0.47  250042   12 end 
时间呼叫线路
<0.01 1 1清除所有
<0.01 1 2清晰等级
3.
1 4对于i=1:250042
0.13 250042 5如果(模(i,1000))
0.12 250 6 fprintf(['current loop:',num2str(i),'\n']);
250 7完
0.24 250042 8结束
9
1 10表示i=1:250042
37.90 250042 11 fprintf(['current loop:',num2str(i),'\n']);
0.47 250042 12结束

打印所有内容都要慢几个数量级。

对fprintf的调用有相当大的开销,特别是对于小的写入操作。例如,执行以下代码:

fid = fopen ( 'a.txt' , 'w+' );

timeStart1000 = tic;
for ( ii = 1 : 10 )
  for ( iii = 1 : 100 )
    b = num2str ( ii );
    fprintf ( fid , b );
  end
end
timeStop1000 = toc ( timeStart1000 );

timeStart10 = tic;
for ( ii = 1 : 10 )
  c = '';
  for ( iii = 1 : 100 )
    c = [ c , num2str(ii) ];
  end
  fprintf ( fid , c );
end
timeStop10 = toc ( timeStart10 );
对fprintf的1000次和10次调用之间存在显著的时间差:
timeStop1000=0.1816
vs.
timeStop10=0.0765

fid = fopen ( 'a.txt' , 'w+' );

timeStart1000 = tic;
for ( ii = 1 : 10 )
  for ( iii = 1 : 100 )
    b = num2str ( ii );
    fprintf ( fid , b );
  end
end
timeStop1000 = toc ( timeStart1000 );

timeStart10 = tic;
for ( ii = 1 : 10 )
  c = '';
  for ( iii = 1 : 100 )
    c = [ c , num2str(ii) ];
  end
  fprintf ( fid , c );
end
timeStop10 = toc ( timeStart10 );