loop performance question

From: pertheli (pertheli_at_hotmail.com)
Date: 01/30/04


Date: 29 Jan 2004 23:00:34 -0800

Hello,
  
  I have a large array of pointer to some object. I have to run test
  such that every possible pair in the array is tested.
  eg. if A,B,C,D are items of the array,
  possible pairs are AB, AC, AD, BC and CD.
  
  I'm using two nested for loop as below, but it is running very slow
  whenever I access any data in the second loop.
  
  I suspect, time taken to fetch the obj2 from memory is the bottleneck
  Is there a way to improve the performance, or accessing data from
  memory any faster in case of obj2?
  
  Thanks in advance
  Pertheli
    
  -----------------------
  // my object
  struct MyObj{
    double a;
    double b;
  };

  // array of *MyObj
  MyObj** objArray = new MyObj*[iMax]; // iMax = large > 20000

  // create MyObj fill pointers int objArray

  // for each possible pair in the array, do some testing
  for (i=0; i<iMax; i++){
    MyObj *obj1 = (MyObj*)objList[i];
  
    for (j=i; j<iMax; j++){
      MyObj *obj2 = (MyObj*)objList[j];
      
      // do some tests
      val += obj1->a; //-> very fast(same obj1)
      val += obj1->b;

      // same number of instruction but very slow
      // accessing from different part in memory??
      //val += obj1->a; //<- obj1 and obj2
      //val += obj2->b; //<- Accessing any data of obj2 is very slow!
                         // 1/10th time slower than the above
    }
  }



Relevant Pages

  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: High Memory Consumption of Classes and Arrays
    ... Only the array itself has overhead. ... memory as a reference type. ... > least consume 40 bytes of memory. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Fast linked list
    ... > amounts of memory rather than huge chunks of it. ... random insertions into a vector/dynamic array are not as slow ... to cause a cache miss. ... some hard numbers on speed differences between lists and arrays. ...
    (microsoft.public.vc.mfc)
  • Re: Fast linked list
    ... > amounts of memory rather than huge chunks of it. ... random insertions into a vector/dynamic array are not as slow ... to cause a cache miss. ... some hard numbers on speed differences between lists and arrays. ...
    (microsoft.public.vc.language)
  • Re: not enough storage... error using GetRows
    ... > array only contained ~150,000 rows. ... It took 19 minutes for GetRows to return (db ... and the prog had consumed 200MB of memory. ... The first one allocates some 180MB, the next two only allocate about 47MB ...
    (microsoft.public.vb.database.ado)