473,721 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<uns igned 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 2976
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<uns igned 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<uns igned 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@okeprea d05...
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<uns igned 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@okeprea d05...
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<uns igned 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
6880
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 another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app performance. In the hopes of improving performance, we have tried to find a way to avoid the...
6
1264
by: Justine | last post by:
Hi All, I need a small clarification with regard to Boxing. struct Currency { ......... } Currency Bal = new Currency; Object Obj = Bal;
0
333
by: Peter Olcott | last post by:
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...
8
1722
by: Peter Olcott | last post by:
Exactly why does C# and .NET require all the extra copying of data?
94
5678
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. Thanks.
19
13718
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
7844
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...
20
2298
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I have found hasnt been encouraging... I found that the *quickest* way is to use a struct to retrieve fixed length packets out of the file, but structs get placed on the stack! In one of the books I read, the stack is only about 1mb, so I would...
0
8858
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8738
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
9376
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
9230
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
9148
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
9085
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
6678
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
4499
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
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.