[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Timing test example between Arrays and Loops



PureBytes Links

Trading Reference Links

Hello Tomasz,

Below is some AFL I wrote to explore the speed difference between  
Array and Loop operations.  I wanted to explore a number of different  
array operations to see if I could speed up my indicators.

It shows the difference in speed for finding the highest value in a  
whole array.
I am not sure my math is correct though, because one of the results  
seem counter intuitive.

For instance:

I test a for() loop that does nothing in order to get the loop  
overhead.  I subtract that number from the loop that is doing the real  
operation to get just the time to do the one operation.  I subtracted  
off the loop overhead because a loop will usually have a lot of  
operations that spread out the overhead.  I do this for 1,000 cycles  
and for 10,000 cycles.

I would expect the time to execute a single AFL line after subtracting  
off the overhead would be the same regardless of the number of  
cycles.  However, the results say that it takes almost twice as long  
to execute that operation in a 1,000 cycle loop than a 10,000 cycle  
loop.  Since this is not what I would expect, so I have to assume I  
did something wrong.  I figured I should ask for a review before  
making statements about how much faster arrays are.

On the other hand, the array operation shows almost one third of the  
time per bar when going from 1,000 to 10,000 bars, which is reasonable.

Am I missing something in my understanding, or am I just a lousy AFL  
coder? :)

Best regards,
Dennis

/*
////////////////////////////////////////////////////////////////////////////////////////
// Simple test code to show speed difference between loop and array  
operations -- 7/4/2009 Dennis Brown
// Edit this AFL for each comparison of equivelent operations
// Check both 1000 and 10,000 bar operations to derive the overhead
// Timing on one pass is jumpy, so it will avarage passes for a large  
term average -- just wait a while
// Watch out for differences in operation lengths due to one way  
nature of SetBarsRequired()
// Do the short cycle length first, then the long one, then edit AFL  
with short one selected to reset SBR -- what a pain
// If I could switch to lower SBR numbers on the fly, I could  
calculate a complete curve with overheads
// Number of bars loaded seems to always be 200 more than the  
requested number with SBR
// Interestingly, //comments seem to run a little faster than /*  
comments */ in the timing loop
*/
LoopText = ArrayText = ""; // init and make global

function LoopOperation( Length ) { /* Edit this function with the loop  
operator to be tested */
	GetPerformanceCounter( True ); /* reset the timer */
	MaxPrice = 0;
	for ( i=0; i<Length; i++ ) {
		/* insert the loop operation(s) to be tested below */
		MaxPrice = Max( MaxPrice, C[i] );
	}
	LoopText = "MaxPrice = Max( MaxPrice, C[i] );" ; /* duplicate  
operator here for display output */
	return round(1000000/Length*GetPerformanceCounter( False ) ); /*  
returns time in microseconds per 1000 */
}

function ArrayOperation( Length ) { /* Edit this function with the  
array operator to be tested */
	GetPerformanceCounter( True ); /* reset the timer */
	MaxPrice = LastValue( HHV( C, Length ) );
	ArrayText = "MaxPrice = LastValue( HHV( C, Length ) );" ; /*  
duplicate operator here for display output */
	return round(1000000/Length*GetPerformanceCounter( False ) ); /*  
returns time in microseconds per 1000 */
}

function LoopOverhead( Length ) { /* loop overhead to be tested, do  
NOT edit */
	GetPerformanceCounter( True ); /* reset the timer */
	for ( i=0; i<Length; i++ ) { ; }
	return round(1000000/Length*GetPerformanceCounter( False ) ); /*  
returns time in microseconds per 1000 */
}

// main routine
LastSBRLength = StaticVarGet( "LastSBRParameterValue" );
SBRLength = Param( "Base cycle length", 1000, 1000, 10000, 9000 );
Length = BarCount-200;
if ( ParamTrigger("Reset Averages", "Click to Reset" ) OR  
LastSBRLength != SBRLength OR Length != SBRLength ) {
	StaticVarSet( "TimeSum0", 0 );
	StaticVarSet( "TimeSum1", 0 );
	StaticVarSet( "TimeSum2", 0 );
	StaticVarSet( "TimeCycles", 0 );
	StaticVarSet( "LastSBRParameterValue", SBRLength );
	Title = " Warning -- SetBarsRequired() can not reduce bars --  Edit  
AFL to reset SBR or increase length parameter";
}
else { // only execute the code if all the bar requirements match so  
we get accurate results (i.e., first cycle is all bars)

	TimeSum0 = StaticVarGet( "TimeSum0" );
	TimeSum1 = StaticVarGet( "TimeSum1" );
	TimeSum2 = StaticVarGet( "TimeSum2" );
	TimeCycles = 1 + StaticVarGet( "TimeCycles" );

	TimeSum0 += LoopOverhead( Length ); // loop overhead to be tested
	TimeSum1 += LoopOperation( Length ); // loop operator to be tested
	TimeSum2 += ArrayOperation( Length ); // array operator to be tested

	StaticVarSet( "TimeSum0", TimeSum0 );
	StaticVarSet( "TimeSum1", TimeSum1 );
	StaticVarSet( "TimeSum2", TimeSum2 );
	StaticVarSet( "TimeCycles", TimeCycles );

	Time0 = round( TimeSum0 / TimeCycles );
	Time1 = round( TimeSum1 / TimeCycles );
	Time2 = round( TimeSum2 / TimeCycles );

	OpTime =  Time1-Time0; // subtract off the constant loop overhead
	speedup = NumToStr( OpTime /Time2, 0.1 );
	if ( Length != SBRLength ) { warning = " !!! Warning Length  
Mismatch !!!"; } else { warning = ""; }

	Title = "Per 1000 bars (cycles) Tming Test\n" +
	"Bars Loaded = " + BarCount + "\n" +
	"Base Cycle Length = " + Length + warning + "\n\n" +
	"Looped Operation:  " + LoopText +
	"\n\n" +
	"Loop Overhead    = " + time0  + "us\n" +
	"Looped Operator = " + OpTime + "us\n\n" +
	"Array Operation:   " + ArrayText +
	"\n\n" +
	"Array Operator      = " + time2 + "us\n" +
	"Speedup Factor   = " + speedup;

} // end of active code

RequestTimedRefresh( 1 );
//Override all bar requirements for our test
SetBarsRequired( SBRLength, sbrAll );



------------------------------------

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/