473,903 Members | 6,689 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 5736

"Mark Wilden" <mw*****@commun itymtm.comwrote in message
news:ub******** ********@TK2MSF TNGP03.phx.gbl. ..
"Peter Olcott" <No****@SeeScre en.comwrote in message
news:mF******** **********@news fe24.lga...
>>
By what factor does a simple integer comparison in a fixed array of integers
to single integer take longer when boxing an unboxing is adding? (Note the
simple comparision itself takes one machine clock). Even if unboxing takes
only one machine clock, we have now doubled the time required.

Doubling a tiny amount of time still results in a tiny amount of time. You'd
have to do a proper performance analysis before determining that
boxing/unboxing was significant.

///ark
How many clock cycles does one unboxing operation take?
Nov 4 '06 #11
Peter Olcott wrote:
"Mark Wilden" <mw*****@commun itymtm.comwrote in message
news:ub******** ********@TK2MSF TNGP03.phx.gbl. ..
>Doubling a tiny amount of time still results in a tiny amount of time. You'd
have to do a proper performance analysis before determining that
boxing/unboxing was significant.
How many clock cycles does one unboxing operation take?
The little program attached below gives on my PC:

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.

Arne
using System;
using System.Collecti ons.Generic;

namespace E
{
public class MainClass
{
private const int N = 10000000;
public static void Main(string[] args)
{
int[] ia = new int[N];
DateTime dt1 = DateTime.Now;
for(int i = 0; i < N; i++)
{
ia[i] = i;
}
for(int i = 0; i < N; i++)
{
if(ia[i] != i)
{
Console.WriteLi ne("Oops");
Environment.Exi t(1);
}
}
DateTime dt2 = DateTime.Now;
object[] oa = new object[N];
Console.WriteLi ne("integer array: " + (dt2 - dt1).TotalSecon ds);
DateTime dt3 = DateTime.Now;
for(int i = 0; i < N; i++)
{
oa[i] = i;
}
for(int i = 0; i < N; i++)
{
if((int)oa[i] != i)
{
Console.WriteLi ne("Oops");
Environment.Exi t(1);
}
}
DateTime dt4 = DateTime.Now;
Console.WriteLi ne("object array: " + (dt4 - dt3).TotalSecon ds);
Console.ReadKey ();
}
}
}
Nov 4 '06 #12
That would make it infeasible. Generics completely bypass boxing and unboxing,
right?

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:45******** *************** @news.sunsite.d k...
Peter Olcott wrote:
>"Mark Wilden" <mw*****@commun itymtm.comwrote in message
news:ub******* *********@TK2MS FTNGP03.phx.gbl ...
>>Doubling a tiny amount of time still results in a tiny amount of time. You'd
have to do a proper performance analysis before determining that
boxing/unboxing was significant.
>How many clock cycles does one unboxing operation take?

The little program attached below gives on my PC:

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.

Arne
using System;
using System.Collecti ons.Generic;

namespace E
{
public class MainClass
{
private const int N = 10000000;
public static void Main(string[] args)
{
int[] ia = new int[N];
DateTime dt1 = DateTime.Now;
for(int i = 0; i < N; i++)
{
ia[i] = i;
}
for(int i = 0; i < N; i++)
{
if(ia[i] != i)
{
Console.WriteLi ne("Oops");
Environment.Exi t(1);
}
}
DateTime dt2 = DateTime.Now;
object[] oa = new object[N];
Console.WriteLi ne("integer array: " + (dt2 - dt1).TotalSecon ds);
DateTime dt3 = DateTime.Now;
for(int i = 0; i < N; i++)
{
oa[i] = i;
}
for(int i = 0; i < N; i++)
{
if((int)oa[i] != i)
{
Console.WriteLi ne("Oops");
Environment.Exi t(1);
}
}
DateTime dt4 = DateTime.Now;
Console.WriteLi ne("object array: " + (dt4 - dt3).TotalSecon ds);
Console.ReadKey ();
}
}
}

Nov 5 '06 #13

Peter Olcott wrote:
That would make it infeasible. Generics completely bypass boxing and unboxing,
right?
Yes. Generics allow the compiler to create aggregate structures that
contain value types directly, rather than structures that contain
Object and so require boxing.

That's why I asked why you weren't using .NET 2.0: generics solve your
problem nicely, but they're only available starting in .NET 2.0.

Nov 5 '06 #14
Peter Olcott <No****@SeeScre en.comwrote:
That would make it infeasible. Generics completely bypass boxing and unboxing,
right?
Assuming you mean using List<intinstead of ArrayList, etc, then yes.

--
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 5 '06 #15

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Peter Olcott <No****@SeeScre en.comwrote:
>That would make it infeasible. Generics completely bypass boxing and
unboxing,
right?

Assuming you mean using List<intinstead of ArrayList, etc, then yes.
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.
--
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 5 '06 #16

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.com.. .
>
Peter Olcott wrote:
>That would make it infeasible. Generics completely bypass boxing and
unboxing,
right?

Yes. Generics allow the compiler to create aggregate structures that
contain value types directly, rather than structures that contain
Object and so require boxing.
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?
>
That's why I asked why you weren't using .NET 2.0: generics solve your
problem nicely, but they're only available starting in .NET 2.0.

Nov 5 '06 #17
Peter Olcott <No****@SeeScre en.comwrote:
Assuming you mean using List<intinstead of ArrayList, etc, then yes.

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.
Well, there will be a *slight* overhead when it comes to accessing
(because it'll need to check the size against the size of the list as
well as then against the size of the backing array) but yes, List<Tis
basically what you want.

--
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 5 '06 #18
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
Nov 5 '06 #19
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.

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 #20

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
9853
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,...
0
11294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10504
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...
1
8057
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
7215
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
6100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4734
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
4316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3329
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.