473,883 Members | 1,682 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 5723

"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
>>integer array: 0,046875
object array: 1,515625

With the uncertainty always present in that kind of micro
benchmarks, that says:

7 million box+unbox per second

Your stuff runs in 0.1 second. If we say that 1/100 overhead
is the acceptable, then you can box and unbox 7000 times in
your code.
>That would make it infeasible. Generics completely bypass boxing and
unboxing, right?

You need to do more than 7000 boxing and unboxing every 1/10 seconds
with less overhead than 1/100 ?

I would call that rather unusual requirements.

Yes - generics will avoid boxing and unboxing, but it will
add some overhead too.

Arne
Take a look a the technology:

--
Patented SeeScreen enables
programs to see anything on
the computer display screen
www.SeeScreen.com
Nov 5 '06 #21

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
Peter Olcott wrote:
>I need the C# equivalent of std::vector. It must be able to dynamically grow
in
size, and it must not have any more overhead than an unmanaged array of
struct.
Can generics handle this?

I would say that the two requirements:
1) be able to dynamically grow
2) must not have any more overhead than an unmanaged array
are in conflict.

Nothing is free.
Unmanaged array access costs no more than unmanaged array access, I think it is
about one clock.
I extended my previous posted example and get:

save retrieve
integer array : 0,08 0,03
object array : 3,05 0,08
array list : 4,67 0,16
generic list : 0,31 0,05

Arne

Nov 5 '06 #22
Peter Olcott wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
>Peter Olcott wrote:
>>I need the C# equivalent of std::vector. It must be able to dynamically grow
in
size, and it must not have any more overhead than an unmanaged array of
struct.
Can generics handle this?
I would say that the two requirements:
1) be able to dynamically grow
2) must not have any more overhead than an unmanaged array
are in conflict.

Nothing is free.

Unmanaged array access costs no more than unmanaged array access, I think it is
about one clock.
The functionality offered by a list will cost. Maybe only in adding
and not in getting. But it will cost.

That should be rather obvious.

Arne
Nov 5 '06 #23

"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:
I need the C# equivalent of std::vector. It must be able to dynamically
grow in
size, and it must not have any more overhead than an unmanaged array of
struct.
Can generics handle this?
I would say that the two requirements:
1) be able to dynamically grow
2) must not have any more overhead than an unmanaged array
are in conflict.

Nothing is free.

Unmanaged array access costs no more than unmanaged array access, I think it
is about one clock.

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 ?
>
That should be rather obvious.

Arne

Nov 6 '06 #24
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.

Certainly I haven't seen anything in .NET which has *no* performance
penalty over using arrays. You could probably write a list which would
return potentially bad data rather than throwing the appropriate
exception if, say, you had a backing array of size 5, a list of size 2,
and asked for element 4. I'd rather have correctness with the tiny
performance penalty though...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 6 '06 #25
If you only store the valueType in ArraryList,may be useedthe template
would be help.
Out of this way ,i couldn't think out what way to fix

May be help for you
On Nov 4, 3:03 am, "Peter Olcott" <NoS...@SeeScre en.comwrote:
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 6 '06 #26

"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.
>
Certainly I haven't seen anything in .NET which has *no* performance
penalty over using arrays. You could probably write a list which would
return potentially bad data rather than throwing the appropriate
exception if, say, you had a backing array of size 5, a list of size 2,
and asked for element 4. I'd rather have correctness with the tiny
performance penalty though...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 6 '06 #27

"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.
Exactly the same is done in .NET when accessing array elements.
Willy.

Nov 6 '06 #28

"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.
Exactly the same is done in .NET when accessing array elements.
Willy.

Nov 6 '06 #29

"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.


Nov 6 '06 #30

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

Similar topics

5
7870
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
3262
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
4504
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
13748
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
7913
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
9803
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
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10835
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
9563
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
7960
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
7114
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
5787
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...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4605
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
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.