Performance of system/dos function

조회 수: 15 (최근 30일)
Xiangrui Li
Xiangrui Li 2017년 5월 3일
댓글: Horshack Horshack 2021년 4월 20일
[status, cmdout] = system(cmd)
has been reported to be slow for a while (see this and this), but there seems no satisfying solution yet.
Here is what I tested for Windows command reg.exe, trying to figure out a workaround.
cmd = 'reg.exe query HKEY_LOCAL_MACHINE\HARDWARE /s'; % as an example
tic; [status, cmdout] = system(cmd); toc
Elapsed time is 3.517736 seconds.
whos cmdout
Name Size Bytes Class Attributes
cmdout 1x95249 190498 char
tic; [status, cmdout] = dos(cmd); toc
Elapsed time is 3.512847 seconds.
tic; cmdout = evalc(['! ' cmd]); toc
Elapsed time is 3.509961 seconds.
As we can see, system(), dos() and evalc('!') have similar performance. When I test the same cmd in other programs (Octave and python), it takes about 0.6 to 0.7 seconds. Running it at Windows command prompt takes less than a second based on my eye test. Then I tried to redirect output to a file:
tic; [status, cmdout] = system([cmd ' >myFile']); toc
Elapsed time is 0.886915 seconds.
This improves the performance significantly. The cmdout is empty now as expected.
Does this suggest there should be a way to improve system() for dealing with cmdout?
Is there a way to redirect cmdout to a Matlab string, so we can avoid to read and delete the temp file?
  댓글 수: 7
Xiangrui Li
Xiangrui Li 2018년 5월 17일
The jsystem at Github has fixed the hanging problem.
Horshack Horshack
Horshack Horshack 2021년 4월 20일
I'm having the same issue. To demonstrate, I wrote a simple C program 'printstdout' that prints the number of characters it's been told to via a command line argument:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
int numCharsToPrint = argc >= 2 ? atoi(argv[1]) : 0;
int numCharsPrintedThisLine;
numCharsPrintedThisLine = 0;
while (numCharsToPrint-- > 0) {
printf("*");
if (++numCharsPrintedThisLine == 78) {
numCharsPrintedThisLine = 0;
if (numCharsToPrint-- > 0)
printf("\n");
}
}
return 0;
}
And tested it with the following script:
numChars = uint32(1);
while (numChars <= 2^20)
fprintf("numChars = %7d: ", numChars);
tic; [exitCode, output] = system(['printstdout ' num2str(numChars)]); toc;
numChars = numChars*2;
end
Here's the output for Matlab:
numChars = 1: Elapsed time is 0.035415 seconds.
numChars = 2: Elapsed time is 0.034140 seconds.
numChars = 4: Elapsed time is 0.033591 seconds.
numChars = 8: Elapsed time is 0.034396 seconds.
numChars = 16: Elapsed time is 0.033863 seconds.
numChars = 32: Elapsed time is 0.033824 seconds.
numChars = 64: Elapsed time is 0.033170 seconds.
numChars = 128: Elapsed time is 0.033617 seconds.
numChars = 256: Elapsed time is 0.033296 seconds.
numChars = 512: Elapsed time is 0.034161 seconds.
numChars = 1024: Elapsed time is 0.032989 seconds.
numChars = 2048: Elapsed time is 0.033904 seconds.
numChars = 4096: Elapsed time is 0.602469 seconds.
numChars = 8192: Elapsed time is 0.703521 seconds.
numChars = 16384: Elapsed time is 0.903753 seconds.
numChars = 32768: Elapsed time is 1.304120 seconds.
numChars = 65536: Elapsed time is 2.105577 seconds.
numChars = 131072: Elapsed time is 3.708676 seconds.
numChars = 262144: Elapsed time is 6.916353 seconds.
numChars = 524288: Elapsed time is 13.246939 seconds.
numChars = 1048576: Elapsed time is 26.452178 seconds.
And here is the output from Octave:
numChars = 1: Elapsed time is 0.0197449 seconds.
numChars = 2: Elapsed time is 0.0113029 seconds.
numChars = 4: Elapsed time is 0.0113032 seconds.
numChars = 8: Elapsed time is 0.0113418 seconds.
numChars = 16: Elapsed time is 0.011956 seconds.
numChars = 32: Elapsed time is 0.010741 seconds.
numChars = 64: Elapsed time is 0.011215 seconds.
numChars = 128: Elapsed time is 0.0108809 seconds.
numChars = 256: Elapsed time is 0.011487 seconds.
numChars = 512: Elapsed time is 0.010798 seconds.
numChars = 1024: Elapsed time is 0.012202 seconds.
numChars = 2048: Elapsed time is 0.0105009 seconds.
numChars = 4096: Elapsed time is 0.0121739 seconds.
numChars = 8192: Elapsed time is 0.01156 seconds.
numChars = 16384: Elapsed time is 0.0121598 seconds.
numChars = 32768: Elapsed time is 0.0127161 seconds.
numChars = 65536: Elapsed time is 0.014755 seconds.
numChars = 131072: Elapsed time is 0.0170429 seconds.
numChars = 262144: Elapsed time is 0.023046 seconds.
numChars = 524288: Elapsed time is 0.0346019 seconds.
numChars = 1048576: Elapsed time is 0.0581162 seconds.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Testing Frameworks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by