Timing Functions
From Jacket Wiki
|
When coding in Jacket it is very useful to have easy access to functions, which can time different functions. To facilitate this we have developed two small functions, which might come in handy. We have two variants with the following characteristics:
- The user can set the minimum time over which the measurement is made. The longer time, the more reliable and reproducible the result is.
- The code ensures that the minimum time set is actually met. It uses a predictor to estimate the needed number of repetitions but it also self updates this to ensure that the minimum time is met.
- Warm-up is included for both MATLAB and Jacket variants.
- Compensation for the repetition loop is included to focus on the function timing and not something irrelevant.
MATLAB Timing
Usage
The input-output relations for the TIMEFUN function is:
function [ TC, TELAPSC, RPTC ] = timefun( FUN, TMIN ) % TIMEFUN Timing of MATLAB function % % INPUT: % FUN : Function handle. % TMIN : Minimum time over which the test is performed. % % OUTPUT: % TC : Execution time of one FUN evaluation. % TELAPSC : Total time over which the test is performed. % RPTC : Number of repetitions used to reach TELAPSC. %
Example 1: Creation of Random Matrix
To create a random matrix of size 400 times 400 in MATLAB and time this event the following is used:
>> [TC, TELAPSC, RPTC] = timefun(@() randn(400,400), 1) TC = 0.0052 TELAPSC = 1.2272 RPTC = 235 >>
Here, TC is the execution time of one instant of randn(400,400), TELAPSC is the total time over which the measurement is done, and RPTC is the number of repetitions the randn() function was executed to use TELAPSC seconds.
Example 2: Timing of Exponential
Suppose we want to know how much time Jacket needs to take the exponential values of a 300 by 300 matrix. In this case we need to make a function, which creates a matrix and does the exponential evaluation afterwards. But since this also includes time need to create the random matrix we need to subtract that time from the former one. The answer can therefore be given as:
>> timefun(@() exp(randn(300,300)), 4) - timefun(@() randn(300,300), 4) ans = 0.0025 >>
Here the evaluation is made over 4 seconds but it is of course possible to try other values. Further down, the same is done for Jacket, and it is thus very easy to compare MATLAB and Jacket performance.
MATLAB Code: TIMEFUN
function [ TC, TELAPSC, RPTC ] = timefun( FUN, TMIN ) % TIMEFUN Timing of MATLAB function % % INPUT: % FUN : Function handle. % TMIN : Minimum time over which the test is performed. % % OUTPUT: % TC : Execution time of one FUN evaluation. % TELAPSC : Total time over which the test is performed. % RPTC : Number of repetitions used to reach TELAPSC. % % USE: % TIMEFUN(FUN) % Measure FUN repeatedly for at least 0.25 seconds (default). % % TIMEFUN(FUN,TMIN) % Measure FUN for at least TMIN seconds (TMIN larger => more accurate). % % [TC, TELAPSC, RPTC] = TIMEFUN(FUN,TMIN) % Measure FUN for at least TMIN seconds and return TC, which is the time % for one execution, and TELAPSC is the totally measurement time. % % Examples: % >> timefun(@() grand(64),1) % % ans = % % 1.7167e-04 % % >> [TC,TELAPSC,RPTC] = timefun(@() rand(500,500)*randn(500,500), 0.9) % % TC = % % 0.0340 % % TELAPSC = % % 0.9191 % % RPTC = % 27 % % Generate error if FUN is not a function handle assert(isa(FUN, 'function_handle')) % Default time to average over if nargin < 2 TMIN = 0.25; end % Estimate time and number of repetitions FUN(); FUN(); ts = tic; FUN(); FUN(); tend = toc(ts)/2; RPTi = max(ceil(0.75*TMIN/tend),2); % Warm up and adjust RPTi estimate ts = tic; for rpt=1:RPTi FUN(); end tend = toc(ts)/RPTi; RPTi = max(ceil(TMIN/tend),2); % Measure time TELAPSC = -1; while TELAPSC < TMIN ts = tic; for rpt=1:RPTi FUN(); end TELAPSC = toc(ts); RPTC = RPTi; RPTi = ceil(1.25*RPTi); end % Compensate for loop time y = FUN(); ts = tic; for ii=1:ceil(1E6/RPTC) for rpt=1:RPTC y; end end tloop = toc(ts)/ceil(1E6/RPTC); TC = (TELAPSC-tloop)/RPTC; end
Jacket Timing
Usage
The input-output relations for the GTIMEFUN function is:
function [ TG, TELAPSG, RPTG ] = gtimefun( FUN, TMIN ) % GTIMEFUN Timing of Jacket function % % INPUT: % FUN : Function handle. % TMIN : Minimum time over which the test is performed. % % OUTPUT: % TG : Execution time of one FUN evaluation. % TELAPSG : Total time over which the test is performed. % RPTG : Number of repetitions used to reach TELAPSG. %
Example 1: Creation of Random Matrix
To create a random matrix of size 400 times 400 in Jacket and time this event the following is used:
>> [TG, TELAPSG, RPTG] = gtimefun(@() grandn(400,400), 1) TG = 4.8652e-04 TELAPSG = 1.2375 RPTG = 2507 >>
Here, TG is the execution time of one instant of grandn(400,400), TELAPSG is the total time over which the measurement is done, and RPTG is the number of repetitions the grandn() function was executed to use TELAPSG seconds.
Example 2: Timing of Exponential
Suppose we want to know how much time Jacket needs to take the exponential values of a 300 by 300 matrix. In this case we need to make a function, which creates a matrix and does the exponential evaluation afterwards. But since this also includes time need to create the random matrix we need to subtract that time from the former one. The answer can therefore be given as:
>> gtimefun(@() exp(grandn(300,300)), 4) - gtimefun(@() grandn(300,300), 4) ans = 1.7829e-04 >>
Here the evaluation is made over 4 seconds but it is of course possible to try other values.
Jacket Code: GTIMEFUN
function [ TG, TELAPSG, RPTG ] = gtimefun( FUN, TMIN ) % GTIMEFUN Timing of Jacket function % % INPUT: % FUN : Function handle. % TMIN : Minimum time over which the test is performed. % % OUTPUT: % TG : Execution time of one FUN evaluation. % TELAPSG : Total time over which the test is performed. % RPTG : Number of repetitions used to reach TELAPSG. % % USE: % GTIMEFUN(FUN) % Measure FUN repeatedly for at least 0.25 seconds (default). % % GTIMEFUN(FUN,TMIN) % Measure FUN for at least TMIN seconds (TMIN larger => more accurate). % % [TG, TELAPSG, RPTG] = GTIMEFUN(FUN,TMIN) % Measure FUN for at least TMIN seconds and return TG, which is the time % for one execution, and TELAPSG is the totally measurement time. % % EXAMPLES: % >> gtimefun(@() grand(64),1) % % ans = 1.7515e-04 % % >> [TG,TELAPSG,RPTG] = gtimefun(@() grand(500,500)*grandn(500,500), 0.9) % % TG = 0.0095 % % TELAPSG = 1.1319 % % RPTG = 119 % % Generate error if FUN is not a function handle assert(isa(FUN, 'function_handle')) % Default time to average over if nargin < 2 TMIN = 0.25; end % Estimate time and number of repetitions geval(FUN()); geval(FUN()); gsync; ts = tic; geval(FUN()); geval(FUN()); gsync; tend = toc(ts)/2; RPTi = max(ceil(0.75*TMIN/tend),2); % Warm up and adjust RPTi estimate gsync, ts = tic; for rpt=1:RPTi geval(FUN()); end gsync; tend = toc(ts)/RPTi; RPTi = max(ceil(TMIN/tend),2); % Measure time TELAPSG = -1; while TELAPSG < TMIN gsync; ts = tic; for rpt=1:RPTi geval(FUN()); end gsync; TELAPSG = toc(ts); RPTG = RPTi; RPTi = ceil(1.25*RPTi); end % Compensate for loop time y = FUN(); gsync, ts = tic; for ii=1:ceil(1E6/RPTG) for rpt=1:RPTG geval(y); end end gsync; tloop = toc(ts)/ceil(1E6/RPTG); TG = (TELAPSG-tloop)/RPTG; end
Go Home: Torben's Corner