473,748 Members | 2,361 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 5681

"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
7858
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
3252
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
4482
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
13724
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
7857
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
9785
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
9530
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
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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
9238
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
8237
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...
0
4593
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
2206
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.