-2

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.

user3532232
  • 257
  • 8
  • 19
  • 2
    Why would you create 1000 pointers? It seems, well, pointless. – juanchopanza Apr 15 '15 at 14:38
  • I'd say [answer is no](http://stackoverflow.com/q/17865166/1997232). – Sinatr Apr 15 '15 at 14:41
  • I think you'd be hardpressed to find any performance increase in C# with this type of approach unless your object init had some sort of drastic performance hit associated with it (such as querying a database), and even then you'd have to deal with the cost at some point. I have a suspicion the compiler would heavily optimize this for you anyway. – David L Apr 15 '15 at 14:42
  • 1
    If there were such a thing it would still need to call the constructor, which is what `new` does ... what problem are you trying to solve? – Alex K. Apr 15 '15 at 14:42
  • The references are used in c# instead of pointers. So why don`t you just create 1000 of them in a loop? – erol yeniaras Apr 15 '15 at 14:53
  • Youd be better of describing *what* you are trying to do rather than *how* . an arrary of LVIs is almost useless – Ňɏssa Pøngjǣrdenlarp Apr 15 '15 at 15:05
  • Well I don't want to call new in each loop since it's allot slower. Never claimed that LVIs are useless just took them as an example, could be an arbitrary class. – user3532232 Apr 16 '15 at 07:59

1 Answers1

2

Actually in this C++ code:

ListViewItem[] items = new ListViewItem[1000];

The object constructor (initialization) will be called 1000 times. That's it, there are a hidden loop.

The only advantage indeed is that the memory allocation is only done once.

  • 1 allocation.
  • n initializations.

There is no equivalent things in C#. The allocation and the initialization are done together, one object at a time, and it's done when you call new.

Your are stuck to:

  • 1 allocation.
  • 1 initialization.
  • repeat.

So use for-loop to achieve it, it's the standard way to achieve it:

var items = new ListViewItem[1000];
for (var i = 0; i < items.Length; i++)
{
    items[i] = new ListViewItem();
}

You can also use linq to impress your partner:

var array = new object[1000];
Enumerable.Range(0, array.Length).Select(i => array[i] = new object());
Orace
  • 7,822
  • 30
  • 45