473,903 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList without Boxing and UnBoxing

How can I create an ArrayList in the older version of .NET that does not require
the expensive Boxing and UnBoxing operations?

In my case it will be an ArrayList of structures of ordinal types.

Thanks.
Nov 3 '06
94 5735

"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:O0******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Peter Olcott" <No****@SeeScre en.comwrote in message
| news:mb******** **********@news fe24.lga...
||
|| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
|| news:u7******** ******@TK2MSFTN GP02.phx.gbl...
|| >
|| "Peter Olcott" <No****@SeeScre en.comwrote in message
|| news:34******** **********@news fe20.lga...
|| |
|| | "Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
|| | news:MP******** *************** *@msnews.micros oft.com...
|| | Peter Olcott <No****@SeeScre en.comwrote:
|| | The functionality offered by a list will cost. Maybe only in
| adding
|| | and not in getting. But it will cost.
|| | >>
|| | >std::vector provides access time equal to the access time of
| unmanaged
|| array
|| | >access, yet can dynamically grow.
|| | >Does .NET have anything with EXACTLY these performance
| characteristics ?
|| | >
|| | Frankly, I doubt that std::vector provides that, to be honest.
| Assuming
|| | it's an array + size, you need to do a size check before the array
|| | access, so there's a performance penalty there, even if it's really
|| | small.
|| |
|| | No there is no size check, and this cost is not be really small, it
at
|| least
|| | doubles the access time.
|| |
|| >
|| >
|| This is definitely not true, consider following small sample:
|| >
|| #include <vector>
|| int main(int argc, char *argv[])
|| {
|| size_t size = 100;
|| std::vector<int array(size); // make room for 10 integers,
|| // and initialize them to 0
|| // do something with them:
|| for(int i=0; i<size; ++i){
|| array[i] = i;
|| }
|| // break here, disassembly follows
|| int v = array[55];
|| return 0;
|| }
|| >
|| comments added [n]
|| >
|| 004025e8 85ff test edi,edi [1]
|| 004025ea 740a je vect!main+0x56 (004025f6)
|| 004025ec 2bdf sub ebx,edi [2]
|| 004025ee c1fb02 sar ebx,2 [3]
|| 004025f1 83fb37 cmp ebx,37h [4]
|| 004025f4 7705 ja vect!main+0x5b (004025fb)
|| 004025f6 e8f1070000 call vect!_invalid_p arameter_noinfo
| (00402dec)
|| 004025fb 8b8fdc000000 mov ecx,dword ptr [edi+0DCh]
|| ds:0023:00324c3 c=00000037 [5]
|| >
|| [1] checks array base pointer for null (non initialized)
|| [2] calculate length of array in bytes (edi = base, edx points to past
| end
|| of array)
|| [3] convert to length in int's (divide by 4)
|| [4] index < length of array
|| [5] move int at edi+55 -ecx
|| >
|| You see, there is a pointer validation check and a bounds check for
each
|| access. This accounts for 6 instructions, supposed index is within
| bounds.
||
|| You are not supposed to get range checking unless you explicitly ask for
| it by
|| invoking the std::vector:at( n) member function.
||
|
| This is an implementation detail. The std library as supplied by MSFT
| (Dinkumware's implementation) ) since 7.1 does range checking, don't know
| about other iplementations though. Point is that you should not rely on
| this, the standard does not enforce operator[] to be 'unchecked' AFAIK.
| The major difference between operator[] and at() access is that the former
| calls a "crt debugger" hook followed by a 'drwatson' call, while the
latter
| throws an "out_of_ran ge" exception.
|
| Willy.
|

Forgot to mention that this behavior can be turned of by means of
the -D_SECURE_SCL switch or in code using:
#define _SECURE_SCL 0
Note that by doing so, you throw away all security related enhancements.

Willy.

Nov 6 '06 #31
Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
>The functionality offered by a list will cost. Maybe only in adding
and not in getting. But it will cost.

std::vector provides access time equal to the access time of unmanaged array
access, yet can dynamically grow.
Does .NET have anything with EXACTLY these performance characteristics ?
Hm.

GCC 3.2 with -O3:

integer array 0.06 0.03
vector 0.33 0.06

BCB 5.6:

integer array 0.06 0.03
vector 0.33 0.05

VC++ 7.1 with /Ox:

integer array 0.06 0.03
vector 0.44 0.05

Results fluctuate a bit, but at average integer array
is faster than vector.

Arne

PS: Ask for code if interested.
Nov 7 '06 #32

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
Peter Olcott wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
>>The functionality offered by a list will cost. Maybe only in adding
and not in getting. But it will cost.

std::vector provides access time equal to the access time of unmanaged array
access, yet can dynamically grow.
Does .NET have anything with EXACTLY these performance characteristics ?

Hm.

GCC 3.2 with -O3:

integer array 0.06 0.03
vector 0.33 0.06

BCB 5.6:

integer array 0.06 0.03
vector 0.33 0.05

VC++ 7.1 with /Ox:

integer array 0.06 0.03
vector 0.44 0.05

Results fluctuate a bit, but at average integer array
is faster than vector.

Arne

PS: Ask for code if interested.
What are the two columns?
Nov 7 '06 #33
Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
>Peter Olcott wrote:
>>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
The functionality offered by a list will cost. Maybe only in adding
and not in getting. But it will cost.
std::vector provides access time equal to the access time of unmanaged array
access, yet can dynamically grow.
Does .NET have anything with EXACTLY these performance characteristics ?
Hm.

GCC 3.2 with -O3:

integer array 0.06 0.03
vector 0.33 0.06

BCB 5.6:

integer array 0.06 0.03
vector 0.33 0.05

VC++ 7.1 with /Ox:

integer array 0.06 0.03
vector 0.44 0.05

Results fluctuate a bit, but at average integer array
is faster than vector.

Arne

PS: Ask for code if interested.

What are the two columns?
Storing and retrieving.

Arne
Nov 8 '06 #34

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
Peter Olcott wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******* *************** *@news.sunsite. dk...
>>Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
The functionality offered by a list will cost. Maybe only in adding
and not in getting. But it will cost.
std::vecto r provides access time equal to the access time of unmanaged
array access, yet can dynamically grow.
Does .NET have anything with EXACTLY these performance characteristics ?
Hm.

GCC 3.2 with -O3:

integer array 0.06 0.03
vector 0.33 0.06

BCB 5.6:

integer array 0.06 0.03
vector 0.33 0.05

VC++ 7.1 with /Ox:

integer array 0.06 0.03
vector 0.44 0.05

Results fluctuate a bit, but at average integer array
is faster than vector.

Arne

PS: Ask for code if interested.

What are the two columns?

Storing and retrieving.

Arne
Is that storing using push_back(), or storing using operator[] ?
I would think that the former would be much slower than the latter.
Nov 8 '06 #35
Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
>Peter Olcott wrote:
>>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45****** *************** **@news.sunsite .dk...
Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
>The functionality offered by a list will cost. Maybe only in adding
>and not in getting. But it will cost.
std::vect or provides access time equal to the access time of unmanaged
array access, yet can dynamically grow.
Does .NET have anything with EXACTLY these performance characteristics ?
Hm.

GCC 3.2 with -O3:

integer array 0.06 0.03
vector 0.33 0.06

BCB 5.6:

integer array 0.06 0.03
vector 0.33 0.05

VC++ 7.1 with /Ox:

integer array 0.06 0.03
vector 0.44 0.05

Results fluctuate a bit, but at average integer array
is faster than vector.
>>What are the two columns?
>Storing and retrieving.
Is that storing using push_back(), or storing using operator[] ?
I would think that the former would be much slower than the latter.
push_back

[] will crash unless the array is preallocated - in which case
a simple array could just as well be used.

Arne
Nov 10 '06 #36

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
Peter Olcott wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******* *************** *@news.sunsite. dk...
>>Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45***** *************** ***@news.sunsit e.dk...
Peter Olcott wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
>>The functionality offered by a list will cost. Maybe only in adding
>>and not in getting. But it will cost.
>std::vecto r provides access time equal to the access time of unmanaged
>array access, yet can dynamically grow.
>Does .NET have anything with EXACTLY these performance characteristics ?
Hm.
>
GCC 3.2 with -O3:
>
integer array 0.06 0.03
vector 0.33 0.06
>
BCB 5.6:
>
integer array 0.06 0.03
vector 0.33 0.05
>
VC++ 7.1 with /Ox:
>
integer array 0.06 0.03
vector 0.44 0.05
>
Results fluctuate a bit, but at average integer array
is faster than vector.
>>>What are the two columns?
>>Storing and retrieving.
>Is that storing using push_back(), or storing using operator[] ?
I would think that the former would be much slower than the latter.

push_back

[] will crash unless the array is preallocated - in which case
a simple array could just as well be used.

Arne
When I am talking about array access, I am talking about reading from the array.
If .NET takes more than 50% more time to read from an array of scalar types, it
would be unacceptable for my application. With Boxing and UnBoxing we have much
more than a 50% cost penalty. I am not sure about the cost penalty (if any) for
generics.
Nov 15 '06 #37
"Peter Olcott" <No****@SeeScre en.comwrote in message
news:QO******** *********@newsf e17.lga...
>
When I am talking about array access, I am talking about reading from
the array. If .NET takes more than 50% more time to read from an array
of scalar types, it would be unacceptable for my application. With
Boxing and UnBoxing we have much more than a 50% cost penalty. I am
not sure about the cost penalty (if any) for generics.
As has been suggested in the past, You should set up a realistic proof
of concept to see if this is the issue that you think it is. If, after
testing, you determine that the performance hit is unacceptable, THEN
you should start working on ways around the issue.

Bill
Nov 15 '06 #38

"Bill Butler" <qw****@asdf.co mwrote in message
news:CaH6h.6165 $dh7.4013@trndd c01...
"Peter Olcott" <No****@SeeScre en.comwrote in message
news:QO******** *********@newsf e17.lga...
>>
When I am talking about array access, I am talking about reading from the
array. If .NET takes more than 50% more time to read from an array of scalar
types, it would be unacceptable for my application. With Boxing and UnBoxing
we have much more than a 50% cost penalty. I am not sure about the cost
penalty (if any) for generics.

As has been suggested in the past, You should set up a realistic proof of
concept to see if this is the issue that you think it is. If, after testing,
you determine that the performance hit is unacceptable, THEN you should start
working on ways around the issue.

Bill
Its not worth spending the learning curve that is required. If I could know in
advance that benchmarks prove that the read access time to scalar types is
comparable to that to read access time of unmanaged array access, then it might
be worth the learning curve to pursue further.
Nov 15 '06 #39
"Peter Olcott" <No****@SeeScre en.comwrote in message
news:4Z******** ***@newsfe22.lg a...
<snip>
Its not worth spending the learning curve that is required. If I could
know in advance that benchmarks prove that the read access time to
scalar types is comparable to that to read access time of unmanaged
array access, then it might be worth the learning curve to pursue
further.
Is your current App running so close to the edge that you cannot afford
any spare clock cycles?

Doing a quick proof of concept shouldn't be THAT time consuming. Perhaps
you really can't afford the overhead, but you will never know if you
don't do a test

Bill

Nov 15 '06 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7871
by: Kevin | last post by:
Hi All Can someone please tell me the difference between an Array and an ArrayList, and where I would be likely to use one over the other Thanks alot for any help
18
3265
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
48
4506
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
19
13751
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
7921
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every other platform in every other language. Normally a value type is the actual data itself stored in...
12
9806
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath As String End Structure
0
9847
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7206
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4727
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.