• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Performance: data access speed (Array, DataSeries)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Performance: data access speed (Array, DataSeries)

    Hello.

    Say, I make my own wighted moving average.

    I have sets of coefficient ( k[2,Period] ).
    On each bar I multiplicate Period last Closes prices at appropriate coefficient.

    I made 3 almost same algorithms:

    Code:
    Variant = "Variant 1";
    for( int i = 0; i < Period; i++ ) 
    { 
    	sum_0 += Close[i] * K[0][i];
    	sum_1 += Close[i] * K[1][i];
    }
    Code:
    Variant = "Variant 2";
    for( int i = 0; i < Period; i++ ) 
    { 
    	double close = Close[i]; 
    	sum_0 += close * K[0][i];
    	sum_1 += close * K[1][i];
    }
    Code:
    Variant = "Variant 3";
    for( int i = 0; i < Period; i++ ) 
    { 
    	double close = Close[i];
    	sum_0 += K[0][i];
    	sum_1 += K[1][i];
    }
    Speed test results (for Period = 10000) on screenshot.

    Variant 1: 0.38 ms \ bar
    Variant 1: 0.33 ms \ bar
    Variant 1: 0.33 ms \ bar

    I want pay attention that "Variant 3" have no multiplication at all - all time spend for data access only.

    My conclusion: calculations take almost no time. All time spend at data access.

    My question: how can we decrease data access time using safe code?
    Attached Files

    #2
    Then I decide to check my conclusion.
    Instead of array of arrays I made array of structures:

    Code:
    struct K_struct
    {
    	public double	m0;
    	public double	m1;
    	public double	m2;
    	public double	m3;
    	public double	m4;
    	public double	m5;
    	public double	m6;
    	public double	m7;
    	public double	m8;
    	public double	m9;
    			
    	public double	n0;
    	public double	n1;
    	public double	n2;
    	public double	n3;
    	public double	n4;
    	public double	n5;
    	public double	n6;
    	public double	n7;
    	public double	n8;
    	public double	n9;
    }
    
    K_struct[] K = new K_struct[Period/10];
    and
    Code:
    Variant = "Struct";
    for( int i = 0; i < Period/10; i++ ) 
    { 
    	double close = Close[i];
    				
    	sum_m += K[i/10].m0 * close;
    	sum_m += K[i/10].m1 * close;
    	sum_m += K[i/10].m2 * close;
    	sum_m += K[i/10].m3 * close;
    	sum_m += K[i/10].m4 * close;
    	sum_m += K[i/10].m5 * close;
    	sum_m += K[i/10].m6 * close;
    	sum_m += K[i/10].m7 * close;
    	sum_m += K[i/10].m8 * close;
    	sum_m += K[i/10].m9 * close;
    	
    	sum_n += K[i/10].n0 * close;
    	sum_n += K[i/10].n1 * close;
    	sum_n += K[i/10].n2 * close;
    	sum_n += K[i/10].n3 * close;
    	sum_n += K[i/10].n4 * close;
    	sum_n += K[i/10].n5 * close;
    	sum_n += K[i/10].n6 * close;
    	sum_n += K[i/10].n7 * close;
    	sum_n += K[i/10].n8 * close;
    	sum_n += K[i/10].n9 * close;
    }
    This works 10 times faster.

    Is there another - more convenient and universal - way to decrease access speed?

    Dll on C++? Dll on assembler? But it is not programmer-friendly.
    Last edited by fx.practic; 03-02-2016, 07:16 AM.

    Comment


      #3
      Slow down a second.

      You need a couple more variants to test.

      Move the declaration out of the loop. Keep the assignment in the loop. (#2).

      And completely remove it in variant#3.

      Hard to exactly know how the compiler optimizes this code if at all without seeing the generated machine language.

      Here is an excellent book, a little old, but it will get you thinking too much.

      http://www.amazon.com/Inner-Loops-So.../dp/0201479605

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by erichuff, Today, 04:53 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by stevenev1, Today, 01:43 PM
      0 responses
      6 views
      0 likes
      Last Post stevenev1  
      Started by shinuvu, Today, 11:05 AM
      3 responses
      13 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by Hagar, Yesterday, 07:12 PM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by PBreen, Today, 11:59 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ShawnB  
      Working...
      X