|
The managed version, when run, always needs to compile your code before running it. This is the main reason for the delays.
I wrote a very complex piece of image processing code in C#, and moved it to C++. The C# version took 5 minutes, the C++ port took days. I had to rethink my approach, the rewritten c++ version takes 4 minutes. The point is, languages are different and care needs to be taken to do reasonable bench tests. One core thing is to make sure the actual application takes long enough for the initial compilation step not to be an issue. To avoid this problem, simply write the code to run twice from a subroutine, with a keypress inbetween. The second time, it will have been precompiled.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi Christian, Thanks for your reply. I am not anti managed code, I think my subject title has given the wrong impression.
My program is quite sophisticated, so I am able to load and start my test scenario after the program has finished loading/ compiling. I can restart/ rerun my test scenario at will.
I did some investigating with a profiler and I found that with the managed version a disproportionate amount of time is spent in periodicity checking. I removed the call to the periodicity checking and this did improve the times, but they were still inconsistant (but not as bad before). So the periodicity checking is exacerbating the problem, but is not the cause.
I did some more timings and I found that the time taken is more consistant (+- 2secs) between test runs within a particular load of the program. The larger variations occur between 'loads' of the program.
Am I right in thinking that all types (even basic types, such as int and double) are on the managed heap in managed code as opposed to the stack with native code? Could this be the cause of the problem?
I did another test run this time with solid guessing turned off (solid guessing reduces the number of times the main iterative loop is run), the native version took 2mins 6secs and the managed version took 6mins 49secs.
Here is the code for the main iterative loop, where Cx and Cy are the point coordinates and TestCount, MaxIters and Bailout have previously been declared and initialised:
<br />
double Zx2;<br />
double Zy2;<br />
<br />
for(TestCount = 0; TestCount < MaxIters; TestCount++)<br />
{<br />
Zx2 = Zx * Zx;<br />
Zy2 = Zy * Zy;<br />
<br />
if(Zx2 + Zy2 > Bailout)<br />
{ break; }<br />
<br />
Zy = 2.0 * Zx * Zy + Cy;<br />
Zx = Zx2 - Zy2 + Cx;<br />
}<br />
In my test scenario it runs for 100,000 iterations per pixel on a 640x480 image, so that is a maximum of 30,720,000,000 iterations (assuming no guessing). Any small inefficiency caused by the managed code will be greatly magnified by the large number of iterations. My code is straight forward, I do not see how a different approach could be anymore efficient.
If this is a shortcoming of managed code then ok, but I was wondering if I was doing anything wrong? Thanks!
zenzero
|
|
|
|
|
Obviously, native code is going to be faster than managed code (in most scenarios). But the differences you quote definitely seem odd. Managed code, after the JIT compilation, should technically be as fast as equivalent native code. But, it again depends on how you've written it. It's hard to say why your managed code is performing so bad without seeing it. Again, taking the exact same C++ code and compiling as /clr would result in MSIL that when JITed would (supposedly) run as fast as its native equivalent, though you may find minor performance differences.
|
|
|
|
|
Hi Nish,
Thanks for your thoughts. Actually both the 'native' and managed versions are compiled with the /clr:pure option, so both are compiled to MSIL. The class structure of the native version is like this:
<br />
class Fractal<br />
{<br />
<br />
public:<br />
virtual int DwellTest(int) = 0;<br />
};<br />
<br />
<br />
class Mandelbrot : public Fractal<br />
{<br />
<br />
public:<br />
int DwellTest(int, int);<br />
};<br />
<br />
<br />
int Mandelbrot::DwellTest(int X, int Y)<br />
{<br />
InitParams(X, Y);<br />
<br />
double Zx2;<br />
double Zy2;<br />
<br />
for(TestCount = 0; TestCount < MaxIters; TestCount++)<br />
{<br />
Zx2 = Zx * Zx;<br />
Zy2 = Zy * Zy;<br />
<br />
if(Zx2 + Zy2 > Bailout)<br />
{ break; }<br />
<br />
Zy = 2.0 * Zx * Zy + Cy;<br />
Zx = Zx2 - Zy2 + Cx;<br />
}<br />
<br />
UpdateState(X, Y);<br />
return TestCount;<br />
}<br />
For the managed version I changed how the classes were declared.
<br />
public ref class Fractal abstract<br />
{<br />
<br />
public:<br />
virtual int DwellTest(int, int) = 0;<br />
};<br />
<br />
<br />
public ref class Mandelbrot : public Fractal<br />
{<br />
<br />
public: <br />
virtual int DwellTest(int, int) override;<br />
};<br />
That probably doesn't help very much!
The reason I was trying to do this is because I wanted to compile it has a class library so that I can use it Mono and create a new platform independent front-end in C# and Mono's implementation of Forms. Unfortunately Mono does not yet support C++/CLI and Micosoft's Windows.Forms is not portable. My other possible solution to the problem was to wrap the native code in C++/CLI but I do not know how to do that for an abstract base class, hence my other question further up
I will probably wait for the Mono team to support C++/CLI
|
|
|
|
|
is it possible to call a native C++ Method from a Managed(?) C++ DLL and pass a std::wstring by ref?
I have a native C++ DLL. This has a static function
<br />
IServer * CreateServer();<br />
I created a Managed C++ DLL, imported the native DLL and now trying to make it work.
<br />
....<br />
[DllImport("DoubleFindDll.dll")]<br />
extern "C" IServer * CreateServer();<br />
<br />
....<br />
IServer srv = CreateServer();<br />
srv->startUp();<br />
std::wstring some_string;<br />
srv->FooBar(some_string);
<br />
FooBar is defined as:
<br />
virtual void FooBar(std::wstring & str) = 0;<br />
and the string is modified inside the method.
Im using VS2005.
Thank you for any hint.
|
|
|
|
|
I need to get a string from an editbox and write it to a binari file. The only problem i'm getting is that my string ends with 0D 0A instead of 00 and it's very important that it ends with 00. Is ther a way to do that easily.
I'm using Cfile and CString only because that the only way i found to write japanese correctly to a binary file and i know it's looks ugly in a managed project.
Here is the importants parts of the code
CFile fVampFile;
char * cInfo = "";
int iEnd;
fVampFile.Open(csFilename,CFile::modeWrite | CFile::shareExclusive | CFile::modeCreate | CFile::typeBinary )
cInfo = (char*)Marshal::StringToHGlobalAnsi(eSpeech->Text).ToPointer();
iEnd = strlen(cInfo);
fVampFile.Write(cInfo,iEnd);
|
|
|
|
|
Hi, I'd need some help with my commandline application.
Its basically a contact list application which allows user to choose to add two types of contacts (club and client).
I wrote the classes for these two. So when the choice is made, a function ADD() is invoked which creates this arrays of type club or client.
The problem is as these object arrays are created within the ADD() function, they have only a local scope so I tried referencing its values so that in the end, the main() function can have have access to them.
But unfortunately the message from the compiler is "referencing an array...."
Is it possible to reference an array so that its scope becomes virtualy global?
If not, what other ways can a variable object within a function be accessed easily by other functions?
|
|
|
|
|
Is this a C++/CLI question, or just C++.
Either way, in this forum or the C++ forum, it sounds like you need to post your code. You can return anything from a method, you can't declare anything in a method and give it scope outside the method without returning it. Returning a pointer makes the most sense ( saves a copy being made ).
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi again...The code is below and i hope it helps clarify my enquiry.I'm new to C++ and all help and suggestions are much appreciated.
*******************code for contact list********************
#include <cstdlib>
#include <iostream>
using namespace std;
class BasicAd{
protected:
string name;
string home_address;
public:
void BasicAd::setValues(string n, string h){
name = n;
home_address = h;
};
void BasicAd::getValues(){
cout<<"name : "<<name<<endl;
cout<<"home address="" :="" "<<home_address<<endl;
};
};=""
class="" clubdir:="" public="" basicad{
private:
string="" member_name;
int="" date_joined;
public:
void="" clubdir::setvalues(string="" n,="" string="" h,="" nm,="" int="" d){
name="n;
home_address" =="" h;
member_name="nm;
date_joined" d;
};
void="" clubdir::getvalues(){
cout<<"name:="" "="" <<name<<endl;
cout<<"home="" "<<home_address<<endl;
cout<<"member="" name:="" "<<member_name<<endl;
cout<<"date="" joined:="" "<<date_joined<<endl<<endl;
};
};="" clientdir:="" client_name;
string="" business_address;
public:
void="" clientdir::setvalues(string="" ha,="" cn,="" ba){
name="n;
home_address" ha;
client_name="cn;
business_address" ba;
};=""
void="" clientdir::getvalues(){
cout<<"\nname:="" "<<home_address<<endl;
cout<<"client="" "<<client_name<<endl;
cout<<"business="" address:="" "<<business_address<<endl<<endl;
};=""
};
void="" add(clubdir&="" [],="" clientdir&="" int&="" size);
void="" edit();
void="" delete();
void="" getoption();
void="" display();
int="" main()
{=""
int&="" sizze;=""
clubdir&="" clubb[size];
clientdir&="" clientt[size];=""
cout<<"....welcome="" to="" the="" contact="" database...."<<endl;
cout<<"-------------------------------------------------"<<endl<<endl;
getoption();
system("pause");
return="" exit_success;
}
void="" add(){=""
string="" name;
string="" ha;
string="" mn;
string="" cn;
string="" ba;
int="" dj;
int="" choice;
clubdir="" club[size];
clientdir="" client[size];=""
cout<<"please="" select="" one="" of="" choices="" perform="" its="" task:="" \nclubdir="" [1]\tclientdir="" [2]:="" ";
cin="">>choice;
if(choice==1){
cout<<"\nEnter number of items for clubDir: ";
cin>>size;
for(int i=0; i<size; i++){
cout<<"\nenter="" values="" of="" clubdir="" "="" <<="" (i+1)="" ":\nname:";
cin="">>name;
cout<<"home address: ";
cin>>ha;
cout<<"member name: ";
cin>>mn;
cout<<"date joined:";
cin>>dj;
cout<<"\n";
club[i].setValues(name, ha, mn, dj);
};
cout<<"\nThe Contact Details addded....";
}
else if(choice==2){
cout<<"\nEnter number of items for clientDir: ";
cin>>size;
for(int i=0; i<size; i++){
cout<<"\nenter="" values="" of="" client="" "="" <<="" (i+1)="" ":\nname:";
cin="">>name;
cout<<"home address: ";
cin>>ha;
cout<<"client name: ";
cin>>cn;
cout<<"business address:";
cin>>ba;
cout<<"\n";
client[i].setValues(name, ha, cn, ba);
};
cout<<"\nThe Contact Details addded....";
};
}; //end of ADD()
void EDIT(){
};
void DELETE(){
};
void DISPLAY(){
};
void getOption(){
int option;
cout<<"Select one of the following options :\nADD - [1] \tEDIT - [2]\t DELETE - [3]: ";
cin>>option;
cout<
|
|
|
|
|
There is an exception caught during loading the Form, but the Form will be loaded no matter what. I want to display that exception Message in front of the Form once the Form is loaded.
If I use message box to display the exception Message inside the Form_load function, the Exception message box is behinde the Form. How can I make it displayed in front of the Form?
|
|
|
|
|
You could add a TextBox to the form and show the message in that TextBox.
|
|
|
|
|
Uhm, the parent form can't have the error message box on it.
I have a class called ErrorScreen displaying error message. Then there is the main menu Form1 (1). the Form invokes function classes(2). These function classes invoke more basic classes(3). These basic classes invoke the ErrorScreen class. But the ErrorScreen instances displays strangely in front of the main menu. The error icon, the text, and the button are not displayed, but empty holes on the ErrorScreen. If the ErrorScreen instances are replaced with Forms::MessageBox::Show(),the error messages are displayed correctly. But I need big size button displayed on the error message screen, therefore I need use the ErrorScreen class created myself.
What senarias I shall look into in order to display those ErrorScreens correctly?
|
|
|
|
|
When I add my assembly to the Toolbox via the Tools | Choose Toolbox Items... dialog, the components in my assembly are added to the General tab. How can I tell Visual Studio 2005 programtically to add my components to some other tab ?
I was expecting some attribute placed before the component class which I could set to tell the Toolbox on which tab I my component would be placed, but I could not find such an attribute. But I am sure their must be some way from within my assembly by which I can direct the Toolbox to place a given component on a particular tab, which would be created for it if the tab does not already exist within the Toolbox.
Edward Diener
|
|
|
|
|
Hello,
I have the following 'effect' with my application, implemented with Managed C++ on Visual Studio 2003.
If I start the application on some computers, MessageBoxes (System.Windows.Forms) are popping up as they are expected to. They show a button and they have a size depending on the message they should show. Only that the text doesn't show. Neither the message nor the OK on the button. The Titlebar is shown correctly. What is causing that kind of problems?
I tested the effect on several PCs already. They all have Windows XP and .NET framework installed (in the correct version). I'm not sure, but I think, the PCs that it's not working on do not have VS2003 installed. However it's working on other PCs that also do not have VS2003.
I made a test application that only gave a MessageBox. It's showing correctly on the development PC but again not on the TestPC(s).
Is it a regional problem? Is it a library problem? Shouldn't those standard library functions (as the MessageBox) not work anywhere. I would doubt if it was my selfmade Messagebox.
Any idea?
Thank you in advance,
dawei
-- modified at 9:38 Wednesday 23rd August, 2006
|
|
|
|
|
Are you passing string literals to the messagebox call? Or are you passing a variable? Try using a constant or a literal - that'd help narrow down the point of error.
|
|
|
|
|
Hi,
yes, I thought of that as well. So I made the test sample using both a constant value ("STRING" as well as S"STRING") and a variable.
Anyway it happens.
I continued my testing and interestingly, although development and testing were made on XP machines, setting the compatibility mode of the application on the test machine stops that error from appearing!
On another website they say that they had similar problems because of a bug in McAfee Anitvirus 8.0. We use that program but our administration always updates that kind of tools. So everyone should have the same version. Hence, either it should work or not work on both machines.
Funny...
Thanks for your answer! It's a strange little problem, but I am happy that at least I have a solution- although not satisfying. If you know the answer, it might also be interesting for other people!
best regards,
dawei
|
|
|
|
|
Is there any way using unasfe (non-managed) raw CPP to temporarily elevate access to s specific function like you can in managed code?
|
|
|
|
|
Ray Cassick wrote: Is there any way using unasfe (non-managed) raw CPP to temporarily elevate access to s specific function like you can in managed code?
Perhaps you could use AdjustTokenPrivileges .
|
|
|
|
|
hi,
i am writing a dll in MC++ for using from C#.
I have a pair of strings and object of class say 'Record', <<string,record>> which should be passed to a dll function. i have learned that SortedList collection class can be used in this scenario. but i dont know how to create SortedList object in C# and how to receive it in managed c++ function. Please guide me on this.
thanks in advance,
Abin
|
|
|
|
|
Can this link help?
Best,
Jun
|
|
|
|
|
yes Jun , it was a very good article
thank u very much !
|
|
|
|
|
I have an unmanaged project that I will to compile in Managed C++. Is it just a matter of adding the /clr switch to the compiler options in VS2005/Project/Properties/C++/Command Line?
Thanks
alias47
|
|
|
|
|
Yes, that is, in theory, correct. However, I'm not sure if that will actually expose anything to the CLR ( if it's a dll ), it will simply make the .NET framework available to your application.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
So, is the solution to create Managed C++ wrapper for each exported routine?
|
|
|
|
|
alias47 wrote: So, is the solution to create Managed C++ wrapper for each exported routine?
What exactly is your scenario? That might help people give a more suitable answer.
|
|
|
|
|