Is this question from a managed perspective? Or do you want to know how to do this in native C++? If the latter this is the wrong forum. If the former, it may be better to ask this in the C# or .NET forum, since your question is not C++/CLI specific, and there are more people answering such questions on those forums.
I have created a program for calculating the Mandelbrot set and other fractals. I have two versions of the program, one where the fractal class is native and another where it is managed. This class contains the main iterative loop for determining whether a point is in the fractal or not; for example, Z = Z^2 + C for the Mandelbrot set. The front end of both versions is managed. The iterative loop is written in straight C++ with no assembler.
I created a test scenario and used a stopwatch to time how long each version of the code took to complete. I found that the native version took consistantly 17.5 seconds +- 0.5 seconds. However, the managed version is inconsistant in the amount of time it takes. It can take 21, 28, 45, or 47 seconds to complete. Why is this? The only thing I can think of is that it is executing the loop in main memory or level 2 cache instead of level 1 cache.
In a second, more intensive, scenario the native version takes 1min 38.5secs and the managed version takes 5mins 11.5sec - this is horrendous!
Does anybody have an ideas? Is it possible to 'fix' the managed version so that it has performance equal to the native version? Any help would be greatly appreciated! Thanks.
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:
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!
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.
For the managed version I changed how the classes were declared.
public ref class Fractal abstract<br />
// Blah blah<br />
virtual int DwellTest(int, int) = 0;<br />
public ref class Mandelbrot : public Fractal<br />
// Blah blah<br />
public: <br />
virtual int DwellTest(int, int) override;<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
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.
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?
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
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.
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.
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.
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!
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.