473,507 Members | 5,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very bad managed C++ performance

Hi All,
I implemented an encryption algorithm using C#, native C++
and managed C++. Then I measured the CPU time used for
executing this algorithm in all implementation. The
managed version of C++ was the worst ! It is 20 times
slower that the native version and 6 times slower that the
C# versio !!
Because i wanted to use automatic memory management
offered by .Net framework, i used the managaed classes
available. e.g. i have used "Array" class instead of
simple "unsigned char []" constuct. It seems that this
replacement is the main factor in decreasing the execution
performance. (I just don't talk about inability of managed
extentions to support jagged arrys for now...;-).
- Do i have to pay such a big penalty for managed
execution ?
- Does anyone have a better solution for
replacing "unsigned char []" ?(except __gc[] which
ultimatly yiels Array too)

Thanks in advance
Nov 16 '05 #1
6 2534
Omid Hodjati wrote:
The managed version of C++ was the worst ! It is 20 times
slower that the native version and 6 times slower that the
C# versio !!
Hi Omid,
Based on these results, the difference between using Managed Extensions for
C++ and C# is suspicious. The performance of the two should be very similar.
- Do i have to pay such a big penalty for managed
execution ?
Of course, the first question whenever talking about performance -- have you
profiled the different applications? It is possible that the arrays may be a
contributing factor to the slow down, but it may not be the only root cause.
Certainly, profiling the C# version and the C++ (managed) version should
illustrate why there is such a big difference.
- Does anyone have a better solution for
replacing "unsigned char []" ?(except __gc[] which
ultimatly yiels Array too)


Without knowing exactly what you are using these arrays for, it's not easy
to suggest an alternative. I do have a few tips for you when using C++. If
verifiability is not an issue, you can use interior pointers to iterate over
the array. This saves you from having bounds checking every time the array
is accessed. If you translate the C# code directly into C++, it will not use
interior pointers and will incur bounds checking.

I hope that gives you some ideas. Cheerio!

--
Brandon Bray Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.
Nov 16 '05 #2
Thank you taking attention Mr. Bray,
The following is one the simples functions in the program.
This function xors an array of bytes. It is so simple but
the managed version is run 20 times slower than unmanaged
version. As you mentioned, range cheking in the array may
be the main problem. I have used __gc[] too. IlDasm shows
that both are interpreted the same way and then the result
is the same.
I have profiled my application execution (using Rational
Quantify). The tool shows that Array class is not a big
factor in perfromance.

-What could be wrong with the solution i have provided in
managed version.

-How can a managed pointer help me to bypass range
checking? (There are restrictions on using managed
pointers that is confusing somehow).

- I appreciate a revised source code for this sample
function.

Thanks in advanced

Omid Hodjati.
////*******C++ unmanaged version******************///
void rvtEncrypt::XOR(unsigned char* ary1,unsigned char*
ary2, int len, unsigned char* res)
{
for(int j=0;j<len;++j)
res[j]=ary1[j]^ary2[j];
}

////*********C ++ managed Version ***************///
void ManagedEncryption::XOR(Array *ary1, Array* ary2, int
len, Array * res )
{
for(int i=0;i<len;++i)
{
Buffer::SetByte(res, i, Buffer::GetByte(ary1, i) ^
Buffer::GetByte(ary2,i));
}
}

//************C# version **********************////
private void XOR (byte[] Ary1, byte[] Ary2, int Len, byte
[] Res)
{
for(int i = 0; i < Len; ++i)
Res[i] = (byte)(Ary1[i] ^ Ary2[i]);
}

Nov 16 '05 #3
Hello,

What stops you from writing exactly the same code as C# version?

For example MC++:

void XOR(Byte Ary1[], Byte Ary2[], int Len, Byte Res[])
{
for(int i = 0; i < Len; ++i)
Res[i] = (Ary1[i] ^ Ary2[i]);
}

I get same results for C#/MC++/VS2003.

In your original code you at the very least add the overhead of calling
SetByte/GetByte/GetByte.
"Omid Hodjati" <om**********@yahoo.com> wrote in message
news:04****************************@phx.gbl...
Thank you taking attention Mr. Bray,
The following is one the simples functions in the program.
This function xors an array of bytes. It is so simple but
the managed version is run 20 times slower than unmanaged
version. As you mentioned, range cheking in the array may
be the main problem. I have used __gc[] too. IlDasm shows
that both are interpreted the same way and then the result
is the same.
I have profiled my application execution (using Rational
Quantify). The tool shows that Array class is not a big
factor in perfromance.

-What could be wrong with the solution i have provided in
managed version.

-How can a managed pointer help me to bypass range
checking? (There are restrictions on using managed
pointers that is confusing somehow).

- I appreciate a revised source code for this sample
function.

Thanks in advanced

Omid Hodjati.
////*******C++ unmanaged version******************///
void rvtEncrypt::XOR(unsigned char* ary1,unsigned char*
ary2, int len, unsigned char* res)
{
for(int j=0;j<len;++j)
res[j]=ary1[j]^ary2[j];
}

////*********C ++ managed Version ***************///
void ManagedEncryption::XOR(Array *ary1, Array* ary2, int
len, Array * res )
{
for(int i=0;i<len;++i)
{
Buffer::SetByte(res, i, Buffer::GetByte(ary1, i) ^
Buffer::GetByte(ary2,i));
}
}

//************C# version **********************////
private void XOR (byte[] Ary1, byte[] Ary2, int Len, byte
[] Res)
{
for(int i = 0; i < Len; ++i)
Res[i] = (byte)(Ary1[i] ^ Ary2[i]);
}

Nov 16 '05 #4
Thank you leon paying attention,

My biggest problem with C++ is Jagged arrays. We can not
create Jagged arrays in Managed C++. In the other parts of
the code i need to create jagged arrays (e.g. an array of
keys). But Managed C++ stops me creating such structures
in Managed heap. I can use "Byte [][] keys" in C# but
there is currently no equivalent in managed C++.
Multidimentional arrays can not fix the problem. Because i
want to pass "one row" of the array to a function (such as
XOR) but it is imposible to pass one row of the array ,i
think.

-I appreciate receiving a better solution

Thanks in advanced
-----Original Message-----
Hello,

What stops you from writing exactly the same code as C# version?
For example MC++:

void XOR(Byte Ary1[], Byte Ary2[], int Len, Byte Res[])
{
for(int i = 0; i < Len; ++i)
Res[i] = (Ary1[i] ^ Ary2[i]);
}

I get same results for C#/MC++/VS2003.

In your original code you at the very least add the overhead of callingSetByte/GetByte/GetByte.
"Omid Hodjati" <om**********@yahoo.com> wrote in message
news:04****************************@phx.gbl...
Thank you taking attention Mr. Bray,
The following is one the simples functions in the program. This function xors an array of bytes. It is so simple but the managed version is run 20 times slower than unmanaged version. As you mentioned, range cheking in the array may be the main problem. I have used __gc[] too. IlDasm shows that both are interpreted the same way and then the result is the same.
I have profiled my application execution (using Rational
Quantify). The tool shows that Array class is not a big
factor in perfromance.

-What could be wrong with the solution i have provided in managed version.

-How can a managed pointer help me to bypass range
checking? (There are restrictions on using managed
pointers that is confusing somehow).

- I appreciate a revised source code for this sample
function.

Thanks in advanced

Omid Hodjati.
////*******C++ unmanaged version******************///
void rvtEncrypt::XOR(unsigned char* ary1,unsigned char*
ary2, int len, unsigned char* res)
{
for(int j=0;j<len;++j)
res[j]=ary1[j]^ary2[j];
}

////*********C ++ managed Version ***************///
void ManagedEncryption::XOR(Array *ary1, Array* ary2, int len, Array * res )
{
for(int i=0;i<len;++i)
{
Buffer::SetByte(res, i, Buffer::GetByte(ary1, i) ^
Buffer::GetByte(ary2,i));
}
}

//************C# version **********************////
private void XOR (byte[] Ary1, byte[] Ary2, int Len, byte [] Res)
{
for(int i = 0; i < Len; ++i)
Res[i] = (byte)(Ary1[i] ^ Ary2[i]);
}

.

Nov 16 '05 #5
Thank you dear Brandon,

You solution was realy a cure!! That was simple and
straight forward. Profiling shows that your scenario is
execuuted much faster. But...

1-The usign this scenario in my whole program requires
some more structure. I have an array of keys in the
program. I want to pass the keys one by one to some
fucntions (such as XOR that you saw befor). I create the
key array using "Byte [][] keys" in C#. I can create them
the same way in unmanaged code too. But i don't know the
equivalent structure in managed world. i can not create
Jagged arrays in managed C++. Do you know any replacement
structure that do not leads to unefficient Array structure
(something replacing "Array* __gc[]" i mean)?

2-Another question dear Brandon. can you help me know what
happens when i compile my unmanaged c++ code usign /clr
option. When i compile my unmanaged c++ project using /clr
the performace of the algorithm degrades 20%. I have no
managed code in this project. Then i expect not to have
extra JITing or verification penalties. I have excluded
startup time from my measurements too. Then i do not
expect such overheads). I do not alocate memory too. The i
do not expect GC overhead too..... it is realy confusing
to me .

3-I Appreciate your attention and help in advance.

-----Original Message-----
Omid Hodjati wrote:
Thank you taking attention Mr. Bray,
Hi Omid, you are welcome! :-)
I have profiled my application execution (using Rational
Quantify). The tool shows that Array class is not a big
factor in perfromance.


The array class probably won't show up in a profiler, but

the functioncontaining the loop you wrote might. Does that show up?
-What could be wrong with the solution i have provided in managed version.
Well, as Leon pointed out, using the strongly typed

arrays in Managed C++helps tremendously. The equivalent of "int[] x" in C# is "int x __gc[]" inC++. System::Array is the base class of all arrays. By using System::Arrayas the type, you're paying an overhead for dynamic type checking everytimeyou use it.
-How can a managed pointer help me to bypass range
checking? (There are restrictions on using managed
pointers that is confusing somehow).
Here's an example that uses interior pointers to avoid

the bounds checking.I'd only use this if profiling determines this is a hot spot. By avoidingbounds checking, you run the danger of passing the wrong length into thefunction (or more problematic, passing arrays of different length into thefunction).

void rvtEncrypt::XOR(unsigned char ary1 __gc[],
unsigned char ary2 __gc[],
int len,
unsigned char res __gc[])
{
unsigned char __gc* pary1 = &ary1[0];
unsigned char __gc* pary2 = &ary2[0];
unsigned char __gc* pres = &res[0];

for (int j=0; j<len; ++j)
{
*res = (*ary1) ^ (*ary2);
pary1++;
pary2++;
pres++;
}
}
- I appreciate a revised source code for this sample
function.
Because arrays contain their length, passing the length

into a function isactually not necessary. For example, "ary1->Length" returns the length ofary1.

Hope that helps. Cheerio!

--
Brandon Bray Visual C++ CompilerThis posting is provided AS IS with no warranties, and confers no rights.

.

Nov 16 '05 #6
So your main question is about jagged arrays and we can disregard
your previous code snippet, which was showing 1d array.
Multidimentional arrays can not fix the problem. Because i
want to pass "one row" of the array to a function (such as
XOR) but it is imposible to pass one row of the array ,i
think.
Here is one way:

void XOR(Byte Ary1[,], Byte Ary2[,], int row, int Len, Byte Res[])
{
for(int i = 0; i < Len; ++i)
Res[i] = (Ary1[row,i] ^ Ary2[row,i]);
}

This runs at least 2x-2.5x times slower than 1d array. But many times
better than Buffer::SetByte/Buffer::GetByte^Buffer::GetByte.

This runs as fast (or faster) than original C# version with 1d array:

void XOR(Byte Ary1[,], Byte Ary2[,], int row, int Len, Byte Res[])
{
unsigned char __pin* pinAry1 = &Ary1[row,0];
unsigned char __pin* pinAry2 = &Ary2[row,0];
for(int i = 0; i < Len; ++i)
Res[i] = (pinAry1[i] ^ pinAry2[i]);
}

I'm sure there are other ideas depending on your design.

Hope that helps
"Omid Hodjati" wrote in message
news:06****************************@phx.gbl... Thank you leon paying attention,

My biggest problem with C++ is Jagged arrays. We can not
create Jagged arrays in Managed C++. In the other parts of
the code i need to create jagged arrays (e.g. an array of
keys). But Managed C++ stops me creating such structures
in Managed heap. I can use "Byte [][] keys" in C# but
there is currently no equivalent in managed C++.
Multidimentional arrays can not fix the problem. Because i
want to pass "one row" of the array to a function (such as
XOR) but it is imposible to pass one row of the array ,i
think.

-I appreciate receiving a better solution

Thanks in advanced
-----Original Message-----
Hello,

What stops you from writing exactly the same code as C#

version?

For example MC++:

void XOR(Byte Ary1[], Byte Ary2[], int Len, Byte Res[])
{
for(int i = 0; i < Len; ++i)
Res[i] = (Ary1[i] ^ Ary2[i]);
}

I get same results for C#/MC++/VS2003.

In your original code you at the very least add the

overhead of calling
SetByte/GetByte/GetByte.
"Omid Hodjati" <om**********@yahoo.com> wrote in message
news:04****************************@phx.gbl...
Thank you taking attention Mr. Bray,
The following is one the simples functions in the program. This function xors an array of bytes. It is so simple but the managed version is run 20 times slower than unmanaged version. As you mentioned, range cheking in the array may be the main problem. I have used __gc[] too. IlDasm shows that both are interpreted the same way and then the result is the same.
I have profiled my application execution (using Rational
Quantify). The tool shows that Array class is not a big
factor in perfromance.

-What could be wrong with the solution i have provided in managed version.

-How can a managed pointer help me to bypass range
checking? (There are restrictions on using managed
pointers that is confusing somehow).

- I appreciate a revised source code for this sample
function.

Thanks in advanced

Omid Hodjati.
////*******C++ unmanaged version******************///
void rvtEncrypt::XOR(unsigned char* ary1,unsigned char*
ary2, int len, unsigned char* res)
{
for(int j=0;j<len;++j)
res[j]=ary1[j]^ary2[j];
}

////*********C ++ managed Version ***************///
void ManagedEncryption::XOR(Array *ary1, Array* ary2, int len, Array * res )
{
for(int i=0;i<len;++i)
{
Buffer::SetByte(res, i, Buffer::GetByte(ary1, i) ^
Buffer::GetByte(ary2,i));
}
}

//************C# version **********************////
private void XOR (byte[] Ary1, byte[] Ary2, int Len, byte [] Res)
{
for(int i = 0; i < Len; ++i)
Res[i] = (byte)(Ary1[i] ^ Ary2[i]);
}

.


Nov 16 '05 #7

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

Similar topics

22
528
by: Alper AKCAYOZ | last post by:
Hello Esteemed Developers and Experts, I have been using Microsoft Visual C++ .NET for 1 year. During this time, I have searhed some topics over internets. Most of the topics about .NET is...
4
1971
by: Nadav | last post by:
Hi, I hope this post will find it's way to some MS technical authority... I am experienced with bought Unmanaged C++ and Managed code ( C# ), There are some issues with the .NET framework that...
4
2495
by: 0to60 | last post by:
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I...
3
3483
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
12
1512
by: Greg | last post by:
For all new desktop apps is managed/CLR recommended? I.e. Native MFC/Win32 not recommended in general for new desktop apps? Speaking in general, not referring to exceptions (e.g. drivers) -- Greg...
5
7237
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C#...
8
8967
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
20
2271
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...
66
3279
by: John | last post by:
Hi What are the advantages actually achieved of managed code? I am not talking of theory but in reality. Thanks Regards
0
7221
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
7109
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
7372
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...
0
5619
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,...
1
5039
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...
0
3190
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...
0
1537
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.