Performance of system/dos function
16 views (last 30 days)
[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.
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?