?
Solved

A couple C++ performance questions

Posted on 2000-04-06
8
Medium Priority
?
197 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:Bill Nolan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 120 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:Bill Nolan
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

718 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