Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #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.
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    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.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by gbourque, Today, 06:39 AM
      0 responses
      2 views
      0 likes
      Last Post gbourque  
      Started by cmtjoancolmenero, Yesterday, 03:58 PM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by benmarkal, Yesterday, 12:52 PM
      3 responses
      23 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by helpwanted, Today, 03:06 AM
      1 response
      20 views
      0 likes
      Last Post sarafuenonly123  
      Started by Brevo, Today, 01:45 AM
      0 responses
      12 views
      0 likes
      Last Post Brevo
      by Brevo
       
      Working...
      X