Back to the main page.

Bug 2225 - speed up warning_once

Reported 2013-07-30 16:07:00 +0200
Modified 2015-01-12 09:24:05 +0100
Product: FieldTrip
Component: core
Version: unspecified
Hardware: PC
Operating System: Mac OS
Importance: P4 normal
Assigned to: Jan-Mathijs Schoffelen
Depends on:
See also:

Robert Oostenveld - 2013-07-30 16:07:27 +0200

Arjen and I noticed that dipole fitting was very slow due to (in the iterative loop) warning_once being called. This caused strcmp to be called many times, which made it very slow. I guess it should be possible to speed it up.

Jörn M. Horschig - 2013-07-31 08:26:02 +0200

hm, in your case, strcmp is called only once per function call (to check whether it is a '-clear' call). I am not aware of any tweaks for that :/

Robert Oostenveld - 2013-07-31 09:01:40 +0200

once per function call can still be very often if the parent function is called in a loop. e.g. consider ---- function test_bug2225 for i=1:1000 issue_warning end % for end % main function function issue_warning warning_once('this warning should not show too often'); end % subfunction ----- assuming something useful is done inside "issue_warning", then the warning handling should not add too much to the execution time.

Jörn M. Horschig - 2013-07-31 09:18:32 +0200

I really wouldn't worry too much about strcmp s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; %find number of random characters to choose from numRands = length(s); %specify length of random string to generate sLength = 50; %generate random string randString = s( round(rand(1,sLength)*numRands) ); tic; for i=1:10000 randString = s( ceil(rand(1,sLength)*numRands) ); if strcmp(randString, '-clear') fprintf('huiii'); end end toc Elapsed time is 0.052663 seconds. After profiling, it seems rather that getsubfield and strtok (as part of the *subfield functions) are taking ~80% of the time

Robert Oostenveld - 2013-07-31 12:42:30 +0200

(In reply to comment #3) > it seems rather that getsubfield and strtok (as part of the > *subfield functions) are taking ~80% of the time So that means that the function can in principle be made 5x faster, i.e. 10ms rather than 50ms per call.

Jan-Mathijs Schoffelen - 2014-09-11 09:51:39 +0200

bash-4.1$ svn add test/test_subfield.m A test/test_subfield.m bash-4.1$ svn commit -m "enhancement - speeding up warning once by addressing several bottlenecks #2225" utilities/getsubfield.m utilities/setsubfield.m utilities/issubfield.m test/test_subfield.m private/warning_once.m private/fixname.m Sending private/fixname.m Sending private/warning_once.m Adding test/test_subfield.m Sending utilities/getsubfield.m Sending utilities/issubfield.m Sending utilities/setsubfield.m Transmitting file data ...... Committed revision 9788. Based on some profiling, the bottleneck here seemed the multiple calls to issubfield,getsubfield,setsubfield and fixname. Using textscan rather than strtok, and dynamic field allocation for the subfield functions speeds up stuff > a factor of 2. The textscan function and dynamic field allocations seem to be present since 2005. Fixname was sped up by replacing a conditional statement, where initially a string was converted to numeric to check numeric-ness, replacing it with checking the int8 representation of the string against the ascii code for numerals makes a big difference in speed.