473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

What is wrong with this?

93
Hi,
I'm confounded. Can anyone please help me? Here are two pieces of code.

This is C++.NET code.
Expand|Select|Wrap|Line Numbers
  1. void CLRStackTest()
  2. {
  3.     int i = 0;
  4.     Stack<int> clrstack = gcnew Stack<int>();
  5.     for(i = 0; i < 1000000; i ++)
  6.     {
  7.         clrstack.Push(i);
  8.     }
  9.  
  10.     while(clrstack.Count > 0)
  11.     {
  12.         clrstack.Pop();
  13.     }
  14. }
  15.  
This is native C++ code.
Expand|Select|Wrap|Line Numbers
  1. void StackTest()
  2. {
  3.     stack<int> *s = new stack<int>;
  4.     for(int i = 0; i < 1000000 ; i++)
  5.     {
  6.         s->push(i);
  7.     }    
  8.     while(!s->empty())
  9.     {
  10.         s->pop();
  11.     }
  12. }
  13.  
I ran the two functions (In seperate projects, 1 a CLR project, the other a win32 console app) and timed.
I got ~30ms for the CLR implementation, and ~660ms for C++ implementation(This was when it was optimized for speed.).
I want to know why it is that .NET implementation performed faster. Are there any stupid mistakes in my code, Is this a bad example, or what else? Thanks in advance.

DumRat
Jan 23 '08 #1
7 1958
weaknessforcats
9,208 Expert Mod 8TB
How did you do your timing??

The CLR is already loaded but a Windows console app has to do all the work itself.
Jan 23 '08 #2
DumRat
93
How did you do your timing??

The CLR is already loaded but a Windows console app has to do all the work itself.
Timing?

I used timeGetTime() for the console app, you know, with timeBeginPeriod() at the beginning. And used DateTime::Now.TickCount for CLR
Jan 24 '08 #3
oler1s
671 Expert 512MB
I used timeGetTime() for the console app, you know, with timeBeginPeriod() at the beginning.
From the MSDN page:
Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution,
I would imagine your timing suffers from resolution problems.

EDIT: Maybe not. If you used it properly, it should be pretty accurate. Let me run some code myself to post something more substantial.
Jan 24 '08 #4
oler1s
671 Expert 512MB
For me, the C++ version was about 55 ms. I don't write Managed C++, but since it compiles down to MSIL, I wrote a C# version, which took about 15-30 ms.

It's pretty close. In case you're wondering about the timing disparity, the C++ version probably loses time in allocating/deallocating memory. C# version probably just preallocates the memory in the beginning and cleans up in the end.
Jan 24 '08 #5
DumRat
93
For me, the C++ version was about 55 ms. I don't write Managed C++, but since it compiles down to MSIL, I wrote a C# version, which took about 15-30 ms.

It's pretty close. In case you're wondering about the timing disparity, the C++ version probably loses time in allocating/deallocating memory. C# version probably just preallocates the memory in the beginning and cleans up in the end.
1.The CLR code takes more time to load and initialize - Is this where the preallocation is done?
2. If that is the case, how can i actually test the 'real' performances?
3. According to you guys, if I were implementing a iterative deepening depth first search using stacks(Say, for 8-puzzle) in both native C++ and CLR, what would be more faster? I always thought that native CPP is faster - if so, by how much?

Thanks for the patience
Jan 24 '08 #6
oler1s
671 Expert 512MB
1.The CLR code takes more time to load and initialize - Is this where the preallocation is done?
Maybe. Both languages significantly abstract from any underlying mechanics. I'm not experienced in .NET to confidently comment on what really happens. Even if I had an idea, a lot of what happens is implementation specific. For example, in the C++ code, which has significantly less abstractions, there's still the question of how stack is implemented, and how new allocates memory.

But it's a good guess that the .NET version allocates a chunk of memory first, and when you time the code, you're timing not much more than the push/pop into the stack, and very limited overhead in keeping tracking of the memory being used. The actual allocation and freeing of memory is done before and after by the garbage collector.

The C++ version is a bit more explicit though. In a sense, there is a memory "manager", created by your compiler for new and delete. This manager allocates memory from the operating system, and then appropriately allows you to make use of it. It's not a garbage collector, as it's up to you to manage the memory given to you, but there is an abstraction layer that is implementation specific.

In any case, you still have to explicitly new and delete memory during runtime. When I was responding to this question, one thing I realized that was my implementation of the C++ code didn't involve a function that ran the stack code. It was all in main. So I would have skipped the initial construction and destruction of the stack (EDIT: in my timing), which no doubt involves allocating and freeing a chunk of memory, and so on.

If that is the case, how can i actually test the 'real' performances?
Which is really hard to do, because there is so much underlying mechanics going on. That's partly why experienced programmers take "benchmarks" with a grain of salt.

If you see any serious comparisons in language, they probably are a whole battery of test code, each focused on various non-trivial problems, like something that involves a lot of arithmetic, something that involves lot of recursive behavior, and so on.

Comparisons are then made across each "category" of tests.

According to you guys, if I were implementing a iterative deepening depth first search using stacks(Say, for 8-puzzle) in both native C++ and CLR, what would be more faster? I always thought that native CPP is faster - if so, by how much?
Maybe the C++ version would be very slightly faster. Properly coded in C++ and in .NET code, the differences should be miniscule. I'm not entirely sure how to dissuade people from looking at speed differences in language as a sort of one dimensional "race".

I would recommend C# over C++ for application development on Windows now. I don't think it's worth spending time on C++ .NET. I believe your first code example was Managed C++, which is already obsolete. The current version is C++ / CLI, which doesn't get all the benefits that C# does anyway.
Jan 24 '08 #7
DumRat
93
Looks like I've been living a lie. Thanks for clearing things out a little. Anyway, I changed the functions a little so that no "Cheat" type of optimizations aren't possible. Even then .NET was faster. Thanks
Jan 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
2
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2;...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.