473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Want to see boxing/unboxing timings - is this code no good?

Hi

Is this class and code snippet not good enough to see the differences
between having a collection of a value type compared with having a
collection of references to a reference type (containing that same
value type)? Using a wrapper around Win32's QueryPerformanceCounter
and some tracing I see very little difference.

class WrappedRectangleF
{
public WrappedRectangleF(RectangleF theRectangleF)
{
_theRectangleF = theRectangleF;
}

public override String ToString()
{
return _theRectangleF.ToString();
}

RectangleF _theRectangleF;
}

FOR BOXING/UNBOXING

int iterations = 100000;
ArrayList container = new ArrayList(iterations);

for (int i = 0; i < iterations; ++i)
{
container.Add(new RectangleF(5, 5, 5, 5));
}
for (int i = 0; i < iterations; ++i)
{
container[i].ToString();
}

COMPARE WITH THE FOLLOWING ...

int iterations = 100000;
ArrayList container = new ArrayList(iterations);

for (int i = 0; i < iterations; ++i)
{
container.Add(new WrappedRectangleF(new RectangleF(5, 5, 5, 5)));
}
for (int i = 0; i < iterations; ++i)
{
container[i].ToString();
}

Any ideas?

Emma Middlebrook
em**************@fastmail.fm
Nov 15 '05 #1
2 1399
In one case you're trying to pass an instance of a value type as an instance
of a reference type. This requires the runtime to box the value type
instance in an instance of a reference-type wrapper. In the other case,
you're essentially doing this work yourself. There shouldn't be much
difference because of the way the test is constructed. What are you trying
to measure?

If you're trying to show the cost of boxing, create two types: one struct
and one class, and contrast the time required to use those types:

struct S { public int val; }
class C { public int val; }

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"emma middlebrook" <em**************@fastmail.fm> wrote in message
news:e2**************************@posting.google.c om...
Hi

Is this class and code snippet not good enough to see the differences
between having a collection of a value type compared with having a
collection of references to a reference type (containing that same
value type)? Using a wrapper around Win32's QueryPerformanceCounter
and some tracing I see very little difference.

class WrappedRectangleF
{
public WrappedRectangleF(RectangleF theRectangleF)
{
_theRectangleF = theRectangleF;
}

public override String ToString()
{
return _theRectangleF.ToString();
}

RectangleF _theRectangleF;
}

FOR BOXING/UNBOXING

int iterations = 100000;
ArrayList container = new ArrayList(iterations);

for (int i = 0; i < iterations; ++i)
{
container.Add(new RectangleF(5, 5, 5, 5));
}
for (int i = 0; i < iterations; ++i)
{
container[i].ToString();
}

COMPARE WITH THE FOLLOWING ...

int iterations = 100000;
ArrayList container = new ArrayList(iterations);

for (int i = 0; i < iterations; ++i)
{
container.Add(new WrappedRectangleF(new RectangleF(5, 5, 5, 5)));
}
for (int i = 0; i < iterations; ++i)
{
container[i].ToString();
}

Any ideas?

Emma Middlebrook
em**************@fastmail.fm

Nov 15 '05 #2
There should be no diffeence in performance between implicit autoboxing and
manually using a wrapper since internally basically the same is done.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #3

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

Similar topics

43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
3
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
94
by: Peter Olcott | last post by:
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. ...
19
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
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.