> Can this be optimized further ?? I know this is the price we have to > pay for using managed code, once container is fully populated we > performed a search,traversing,hot spot insertion etc... the difference > between .NET & VC++ was negligible but SGL was at its best. > unfortunately SGL is supported only in VC++. > > We are developing a prototype for a mission critical financial > application This has led to an unwieldy situation... Whether the > components needs to be written in C# or VC++ and performance is the key > issue which needs to be addressed. > > > Enclosed below is .NET Code > =================== > ArrayList ElementObject= new ArrayList(); > int Counter; > DateTime StartTime,EndTime; > TimeSpan DiffValue; > StartTime = DateTime.Now; > for ( Counter = 0;Counter<=5000000;Counter++) > { > ElementObject.Add(Counter); > } > EndTime = DateTime.Now; > DiffValue = EndTime - StartTime; > Console.WriteLine("{0} > {1}",DiffValue.Milliseconds.ToString(),DiffValue.ToString());
As a previous poster mentioned, the ArrayList is dynamically growing, which is leading to some of the inefficiency. The other unnecessary inefficiency is the storage of int datatypes in the ArrayList. The ArrayList stores everything as an object, so integers must be boxed (wrapped in a class wrapper) prior to insertion, and unboxed upon retrieval. You can fix the dynamic growth issue by initializing the capacity of the ArrayList when you create it: ArrayList ElementObject = new ArrayList(5000000); But the boxing issue is not as easily dealt with. If, in your final design, you intend to store class references in the array list instead of integers (or any value type), then change your test to store class references, and your performance discrepancies should all but disappear. If you do intend on storing integers (or doubles, or floats, or enums, or structs, ...), then you are better off using a native array, or check out .NET Collection Gen available at [1] if you need more than a plain array. -Fritz [1] http://www.sellsbrothers.com/tools/ Fritz Onion DevelopMentor http://staff.develop.com/onion/ You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.