Solved

A couple C++ performance questions

Posted on 2000-04-06
8
187 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now