Solved

A couple C++ performance questions

Posted on 2000-04-06
8
189 Views
Last Modified: 2010-04-02
A couple performance questions:
Is there any performance difference between using constants and variables?  Or is this merely a logical/organizational issue...

Also, is there any performance difference between using heap and stack memory?

Thanks!
0
Comment
Question by:Slimfinger
8 Comments
 
LVL 2

Expert Comment

by:freshmeat
ID: 2692033
ha, sit with u and wait for the answers together
^@^
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2692080
On your first question, I guess I would say yes, IF one constitutes performance as "any amount of time for the code to run longer/shorter".  
Here's a quick example:
const int x=5;
int z=5;
....
"In some function now"
int y=6;
if (y==x) {do whatever}

Because x is a constant, the compiler doesn't need to generate code to retrieve it's value and can put in the assembly code the value 5 for x and simply do a
   cmp "address of y",5

IF you write
if (y==z) {do whatever}

the compiler is clueless as to the value of z, so it must generate assembly code, which uses machine cycles, to retrieve its value.  On my machine the code generated is
   mov ecx,"address of y"
   cmp ecx,"address of z"

As you can see it takes two instructions now and two retrievals from memory.  BUT, how many nanoseconds did we save by using x versus z in this one compare?  Atleast a few but I hate to even think about how "long" a nanosecond is.
 
   I also look forward to hearing responses to this question.
   Glenn

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 40 total points
ID: 2692168
big diff in both cases.

between const and variables...

To access the value of a variable a program has to go to a memeory location and get it.  With a constant, the compiler knows the value, can optimise it (constant folding) and can load the value directly into processor registers etc.

between heap and stack...

heap is slower as the memory has to be searched for and allocated, and later deleted and returned to free storage.  With the local variable stack, there is no allocation/deallocation.  The next chunk in the stack area is used (known location), similarly, when finished the stack pointer is just moved back.

If you have a choice, prefer constant and local (stack) storage.  But note that unless you are really going for speed in some critical part of your program, always choose the one that is easiest to write and amatain and gives you the nicest algortihm.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 7

Author Comment

by:Slimfinger
ID: 2692241
Just one question about heap/stack.  Would it be true, then, that there is no difference in the performance between these two aside from alloaction/deallocation?  For example, if these variables were member variables of a class and persisted throughout the application, would there still be any performance difference in merely ACCESSING the variables/mem space, discluding the allocation/deallocation of the space?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2692258
if an object is created on the stack, you need to dereference through a pointer.

if it is a member variable or a local function variable (on the stack), then the access to the value will be quicker.

Also note that allocating and freeing lots of objects fragments storage and generally makes things start to run slower.  stack allocation doesn't have  this disadvantage.

Again, the differences are not that great except for time-critical code when you are trying to squeeze the most out of performance.

There are also programming issues .. when using the heap you have to explicitly create AND destroy objects, with teh stack,things disappear automatically when they go out of scope.

In summary, if you have a choice, always choose constants over variables and local/member over heap.  Unless doing so make your program harder to write/maintain.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2692261
ooops .. that should have read

if an object is created on the HEAP, you need to dereference through a pointer.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2693042
A few additions, you are not guaranteed that using a constant as opposed to a variable will allways be faster, but it is likely to be faster, and it is very very unlikely to ever be slower.  

Heap memory is slow because of the time needed to perform allocations and deletions.  It is not slower to access, provided that you access the heap memery in the same manner as you are acessing the "other memory".  Since you have to using a pointer to access the heap memory, it will takes just as long to acess non-heap memory with a pointer.

But the difference between accessing a global varaibles, a non-static (stack-based) local variable and a dynamically allocated variable (heap-based) are a few clock cycles.  On modern computers we talking about a difference of something like 100 millionth of a second.  You will never notice this sort of difference except in the most time critical portions of the program (like loops that execute for long periods of time), and probably not even then.

RONSLOW, I think it is beyond the scope of this question, but member variables will usually be accessed via a pointer, the this pointer.  So they will  usually give about the same performance as any other indirect data.  In VC/BCB though, there is a small savings because the this pointer is usually in ECX so there is often no need to get it off the stack before dereferencing, so it saves an indirection--sometimes.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2695068
nietod.  member vars that are POINTERS will be slower than just member vars. that was the point I was (trying to)make.  Also, within a member function in particular, access to member vars is not so bad due to registers holding this (as you pointed out).

If one DOES want some etra speed in some time critical code (a tight loop for eg), one could copy from the member var to a local to improve speef.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 55
Focus not getting shifted out of  editbox 2 62
IdTelnet1DataAvailable handler freezes the Application 4 74
Safe conversion? 4 67
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question