469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Can Managed C++ Eliminate Boxing/UnBoxing Overhead?

I want to be able to make my .NET applications run
just as fast as unmanaged C++. From my currently
somewhat limited understanding of the .NET framework
and the C# language, it seems that Boxing/Unboxing
might present of problem. Since C++ has pointer syntax,
I was thinking that this might eliminate the need for
Boxing and Unboxing. Am I right?

One of the things that my application needs is something
exactly like std::vector<unsigned int>. In C++ this has
the same performance as an array of "int". Is there
something like this in C#, or can I use managed C++
and a std::vector of managed memory?

Nov 17 '05 #1
4 2651
Peter Olcott wrote:
I want to be able to make my .NET applications run
just as fast as unmanaged C++. From my currently
somewhat limited understanding of the .NET framework
and the C# language, it seems that Boxing/Unboxing
might present of problem. Since C++ has pointer syntax,
I was thinking that this might eliminate the need for
Boxing and Unboxing. Am I right?

One of the things that my application needs is something
exactly like std::vector<unsigned int>. In C++ this has
the same performance as an array of "int". Is there
something like this in C#, or can I use managed C++
and a std::vector of managed memory?


You are right that boxing may be one of the points where the performance
of your .NET app suffers, but AFAIK you can't change that by using
(managed) C++ as a language. C# can use pointers in unsafe code and
managed C++ doesn't have anything else than that to offer either. The
issue of boxing doesn't have anything to do with which language you are
using.

The STL collection sample you give makes use of C++ templates to provide
for typed collections. You are correct in assuming that typed collections
will get rid of boxing overhead - you can do the exact same thing with
..NET Generics in .NET 2 (constructs like List<uint>). I'm lacking the
experience with managed C++ to say whether or not it's possible to make
use of C++ templates in managed code - I guess it might be possible
because C++ templates are a compiler feature, as opposed to .NET Generics,
which are a runtime feature. Given the choice, I'd opt for Generics of
course, if only for compatibility with other .NET languages.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #2
Peter Olcott wrote:
I want to be able to make my .NET applications run
just as fast as unmanaged C++. From my currently
somewhat limited understanding of the .NET framework
and the C# language, it seems that Boxing/Unboxing
might present of problem. Since C++ has pointer syntax,
I was thinking that this might eliminate the need for
Boxing and Unboxing. Am I right?

One of the things that my application needs is something
exactly like std::vector<unsigned int>. In C++ this has
the same performance as an array of "int". Is there
something like this in C#, or can I use managed C++
and a std::vector of managed memory?


You are right that boxing may be one of the points where the performance
of your .NET app suffers, but AFAIK you can't change that by using
(managed) C++ as a language. C# can use pointers in unsafe code and
managed C++ doesn't have anything else than that to offer either. The
issue of boxing doesn't have anything to do with which language you are
using.

The STL collection sample you give makes use of C++ templates to provide
for typed collections. You are correct in assuming that typed collections
will get rid of boxing overhead - you can do the exact same thing with
..NET Generics in .NET 2 (constructs like List<uint>). I'm lacking the
experience with managed C++ to say whether or not it's possible to make
use of C++ templates in managed code - I guess it might be possible
because C++ templates are a compiler feature, as opposed to .NET Generics,
which are a runtime feature. Given the choice, I'd opt for Generics of
course, if only for compatibility with other .NET languages.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3
"Peter Olcott" <ol****@att.net> wrote in message
news:VGgRe.6139$UI.719@okepread05...
I want to be able to make my .NET applications run
just as fast as unmanaged C++.
Good luck ;)
From my currently
somewhat limited understanding of the .NET framework
and the C# language, it seems that Boxing/Unboxing
might present of problem. Since C++ has pointer syntax,
I was thinking that this might eliminate the need for
Boxing and Unboxing. Am I right?
Boxing is an overhead, no doubt about it, and given that C# has autoboxing
instead of manual boxing it can happen when you don't expect it. If you want
to spend time optimising, just write your own collection that doesn't box
anything. C# has pointers too if you really want to use them. I was writing
a numerical algorithm a few years ago in C# and used pointer arithmetic to
get faster data access. I also pinned and passed managed arrays to native
C++ code for the really nasty stuff.

One of the things that my application needs is something
exactly like std::vector<unsigned int>. In C++ this has
the same performance as an array of "int". Is there
something like this in C#, or can I use managed C++
and a std::vector of managed memory?


You can use std::vectors for managed data, but there's really no need to.
Writing your own none boxing collection in v1.1 of the framework is trivial.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #4
"Peter Olcott" <ol****@att.net> wrote in message
news:VGgRe.6139$UI.719@okepread05...
I want to be able to make my .NET applications run
just as fast as unmanaged C++.
Good luck ;)
From my currently
somewhat limited understanding of the .NET framework
and the C# language, it seems that Boxing/Unboxing
might present of problem. Since C++ has pointer syntax,
I was thinking that this might eliminate the need for
Boxing and Unboxing. Am I right?
Boxing is an overhead, no doubt about it, and given that C# has autoboxing
instead of manual boxing it can happen when you don't expect it. If you want
to spend time optimising, just write your own collection that doesn't box
anything. C# has pointers too if you really want to use them. I was writing
a numerical algorithm a few years ago in C# and used pointer arithmetic to
get faster data access. I also pinned and passed managed arrays to native
C++ code for the really nasty stuff.

One of the things that my application needs is something
exactly like std::vector<unsigned int>. In C++ this has
the same performance as an array of "int". Is there
something like this in C#, or can I use managed C++
and a std::vector of managed memory?


You can use std::vectors for managed data, but there's really no need to.
Writing your own none boxing collection in v1.1 of the framework is trivial.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Mountain Bikn' Guy | last post: by
6 posts views Thread by Justine | last post: by
reply views Thread by Peter Olcott | last post: by
8 posts views Thread by Peter Olcott | last post: by
94 posts views Thread by Peter Olcott | last post: by
19 posts views Thread by ahjiang | last post: by
161 posts views Thread by Peter Olcott | last post: by
20 posts views Thread by =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.