cli::array and c++ dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahxephon
    New Member
    • Mar 2009
    • 5

    cli::array and c++ dynamic array

    I am new to CLI/c++ and have some question on the 2 type of array stated above. I am doing some numerical calculation on large number of sample and face some problem with the speed on accessing the data in array.

    As example, accessing data from
    Code:
    array<int>^ data = gcnew array<int>(1000000);
    for(int i = 0; i < data->GetLength(0); i++){[INDENT]data[i] = i;[/INDENT]
    }
    is far slower compare to

    Code:
    int *data;
    data = new int[1000000];
    for(int j = 0; j <1000000; j++){[INDENT]data[j] = j;[/INDENT]
    }
    I wrote finish a code using dynamic array and it run faster compare to the one using CLI::array.

    Anyone know why this happen? Is it the way i write my code wrong? Currently working in console enviroment but will move to GUI so i plan to change all the the array to CLI::array. Would C# be better if i plan to construct the program?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You did just fine. The slowdown comes with gcnew. This is a garbage-collected array and the garbage collector needs time. The C++ new operator has none of that. Instead, you have the resonsibility to clean up after yourself.

    Managed code is great when time is not an issue.

    As a side note have you considered creating private process heap and allocating from that? You can use HeapCreate and then when you are done you just delete the heap and there go your leaks.

    Comment

    • rahxephon
      New Member
      • Mar 2009
      • 5

      #3
      Thanks for the reply. I am new to programming in windows, so I am trying out few things for the program. Will try to use HeapCreate to see how it goes.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Get a copy of Windows viaC/C++ by Jeffrey Richter 2007. Pay attention to page 528.

        Comment

        • rahxephon
          New Member
          • Mar 2009
          • 5

          #5
          Just found the book in my local library. Will look through it and see how it goes.

          Comment

          Working...