So I want to instantiate a chunk of objects to gain some speed instead of creating each item with the new keyword. Eg. in c++ you'd use the code below:
ListViewItem[] items = new ListViewItem[1000];
But I can't get my head around how to do this in c#. If I excecute the code above I just get 1000 pointers.(I guess since they are null)
My goal is to avoid having to call new 1000 times
Edit: Made a testprogram in c++ to try to show what I want to achieve in c#.
#include "stdafx.h"
#include <vector>
#include <string>
#include <time.h>
#include <iostream>
using namespace std;
class A
{
public:
A()
{
m_pVector = NULL;
}
void setVector(vector<string>* pVector)
{
m_pVector = pVector;
}
void deleteVector()
{
vector<string>* temp = m_pVector;
m_pVector = NULL;
delete temp;
}
private:
vector<string>* m_pVector;
};
int _tmain(int argc, _TCHAR* argv[])
{
int size = 100000;
A* A1s = new A[size]();
A* A2s = new A[size]();
long int start1=0;
long int diff1=0;
long int start2=0;
long int diff2=0;
vector<string>* mVectors = NULL;
for(int c1=0;c1<10;c1++)
{
if(c1>0)
{
vector<string>* temp1 = mVectors;
mVectors = NULL;
delete[] temp1;
for(int i=0;i<size;i++)
{
A2s[i].deleteVector();
}
}
start1 = clock();
mVectors = new vector<string>[size]();
for(int i=0;i<size;i++)
{
A1s[i].setVector(&mVectors[i]);
}
diff1 = clock() - start1;
start2 = clock();
for(int i=0;i<size;i++)
{
A2s[i].setVector(new vector<string>());
}
diff2 = clock() - start2;
cout<<"1:"<<diff1<<" 2:"<<diff2<<" diff:"<<diff2-diff1<<endl;
}
cin.get();
}
So createing a chunk of data is allot faster than creating new in each loop. And I'd like to do the same in c# but I can't figure out how to create the chunk of data.