PC Review


Reply
Thread Tools Rate Thread

Boxing Question

 
 
Nathan Neitzke
Guest
Posts: n/a
 
      14th Mar 2005
I am writing an app that might largely benefit from using a struct instead
of a class. However, it needs to be rich enough where there are methods
available.

My question is - every time you call a method on a struct, is it boxed?
Because that would be a huge perf hit.

I am assuming that it does have to be boxed, but if anyone knows for sure
that would be great!


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      14th Mar 2005
Nathan,

>I am writing an app that might largely benefit from using a struct instead
>of a class.


Why?


>My question is - every time you call a method on a struct, is it boxed?


No



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      15th Mar 2005
Hi Nathan,

>I am writing an app that might largely benefit from using a struct instead
>of a class.


There is little or no performance reason to prefer a struct over a class,
little or no efficiency reason, and little or no readability reason.
Structs are useful for making API calls to unmanaged code.

Structs are useful in only the smallest number of cases, and personally,
I've never used them in C# outside of unmanaged API calls. In fact, I've
never even _seen_ them used in the hundreds of thousands of lines of code
that have been delivered in the systems that I've overseen, reviewed, or
participated in.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nathan Neitzke" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
However, it needs to be rich enough where there are methods
> available.
>
> My question is - every time you call a method on a struct, is it boxed?
> Because that would be a huge perf hit.
>
> I am assuming that it does have to be boxed, but if anyone knows for sure
> that would be great!
>



 
Reply With Quote
 
Ajay Kalra
Guest
Posts: n/a
 
      15th Mar 2005
>There is little or no performance reason to prefer a struct over a
class,
>little or no efficiency reason


Correct me if I am wrong, but struct/value type in managed code is
mainly to improve performance. If you have large amount of data,
creating it on the stack is considerably less expensive than going on
the heap.

I have personally never used structs in my code (which is not really
saying much) but I have always been looking for some reason which will
justify it based solely from performance point of view. I have always
thought of the sceanrio where a large number of objects are getting
allocated/deallocated to be the driving force behind using a value type
in managed code.

---------
Ajay Kalra
(E-Mail Removed)

 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      15th Mar 2005
"Nick Malik [Microsoft]" <(E-Mail Removed)> wrote in message
news:IpKdnYVTtrkdaavfRVn-(E-Mail Removed)...
....
> Structs are useful in only the smallest number of cases, and personally,
> I've never used them in C# outside of unmanaged API calls. In fact, I've
> never even _seen_ them used in the hundreds of thousands of lines of code
> that have been delivered in the systems that I've overseen, reviewed, or
> participated in.



I can think of cases where using classes instead of structs would make a
semantic difference. In the example below, if Currency was a reference type
the price of the product would be increased too, which is wrong:

lineItem.Price = product.Price;
lineItem.Price += new Currency(20, "CHF");

But as you Nick, I never saw it in real systems...


Alexander


 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      15th Mar 2005
Oops, it should be:

lineItem.Price = product.Price;
lineItem.Price += new Currency(20, "CHF");


 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      15th Mar 2005
Argh...

Something somewhere eats my plus signs!

Anyway it's the increment on the second line, "plus equals".

"Alexander Shirshov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Oops, it should be:
>
> lineItem.Price = product.Price;
> lineItem.Price = new Currency(20, "CHF");
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Mar 2005
Nick Malik [Microsoft] <(E-Mail Removed)> wrote:
> >I am writing an app that might largely benefit from using a struct instead
> >of a class.

>
> There is little or no performance reason to prefer a struct over a class,
> little or no efficiency reason, and little or no readability reason.
> Structs are useful for making API calls to unmanaged code.


So you'd be happy if Int32 were a reference type, for example? I have
to disagree - the performance cost there would be dreadful. Imagine:

for (int i=0; i < 100000; i++)
{
....
}

You'd have created 100,000 objects on the heap, just for the iteration!

> Structs are useful in only the smallest number of cases, and personally,
> I've never used them in C# outside of unmanaged API calls. In fact, I've
> never even _seen_ them used in the hundreds of thousands of lines of code
> that have been delivered in the systems that I've overseen, reviewed, or
> participated in.


I agree that they're very rarely useful outside interop, but they do
occasionally have their uses. I have two examples:

1) http://www.pobox.com/~skeet/csharp/t...ernative.shtml

2) Things which are units of measure. I've recently been writing some
code to do with coverage, and it's handy to have a Coverage struct
which is basically two ints ("total sequence points" and "hit sequence
points"). It's logically a value type in the same way that Int32 is,
and it's nice not to have to create a new (separate) object on the heap
every time I create one of them.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      15th Mar 2005
Nathan,

I find that about boxing and unboxing always very interesting.

I made a simple test, can you try it.
In this test there is by the way 400,000,000 times a boxing or an unboxing
the results are milliseconds

{
int end1=0;
int end2=0;
for (int y = 0; y<1000;y++)
{
int start1 = Environment.TickCount;
for (object i=0; (int)i < 100000; i = (int) i + 1)
//This is very inefficient because the increment of the indexer
{
//Generated ILS code
//IL_000d: stloc.3
//IL_000e: ldc.i4.0
//IL_000f: box [mscorlib]System.Int32
//IL_0014: stloc.s i
//IL_0016: br.s IL_0029
//IL_0018: ldloc.s i
//IL_001a: unbox [mscorlib]System.Int32
//IL_001f: ldind.i4
//IL_0020: ldc.i4.1
//IL_0021: add
//IL_0022: box [mscorlib]System.Int32
//IL_0027: stloc.s i
//IL_0029: ldloc.s i
//IL_002b: unbox [mscorlib]System.Int32
//IL_0030: ldind.i4
//IL_0031: ldc.i4 0x186a0
//IL_0036: blt.s IL_0018
//IL_0038: ldloc.0
}
end1 += Environment.TickCount-start1;
int start2 = Environment.TickCount;
for (int i = 0; i < 100000; i++)
{
//Generated Ils code
//IL_003e: ldloc.3
//IL_003f: sub
//IL_0040: add
//IL_0041: stloc.0
}
end2 += Environment.TickCount-start2;
}
MessageBox.Show("Object: " + end1.ToString()
+ " Integer: " + end2.ToString());
}

Cor


 
Reply With Quote
 
Jeff Louie
Guest
Posts: n/a
 
      16th Mar 2005
I occassionaly return an error struct with a bool field.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on boxing Jay Microsoft C# .NET 5 9th Aug 2007 08:45 AM
Question on boxing Mr Flibble Microsoft C# .NET 2 12th Jul 2006 11:25 AM
Simple boxing question Mike D Sutton Microsoft C# .NET 2 12th Sep 2005 01:04 AM
Question about Boxing Sam Kong Microsoft C# .NET 1 17th Mar 2005 06:39 PM
Difference between boxing a primitive type and boxing a custom str =?Utf-8?B?RGF2aWQgU2hlbg==?= Microsoft Dot NET Framework 5 13th Nov 2004 04:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:45 AM.