Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-07-2005, 02:07 AM   #1
Registered: Nov 2003
Posts: 55

Rep: Reputation: 15
C++: #define OR const int ?


If SPEED is the most important thing in a program,

which is faster

#define wordSize 256
const int wordSize = 256;

char word[wordSize]; // This line is called many times

and please explain why?

Thanks in advance for your time and help.
Old 10-07-2005, 02:10 AM   #2
LQ Newbie
Registered: Sep 2005
Posts: 2

Rep: Reputation: 0
It's the same thing on every decent compiler.
The only time you may save is some nanoseconds during compile time
Old 10-07-2005, 02:34 AM   #3
Registered: Nov 2003
Posts: 55

Original Poster
Rep: Reputation: 15
Thanks for your reply.

I know my question might seem trivial but
I care about Every nanosecond at RUN time.
I don't care about compile time.

Could anyone assure me that at run-time it will be the same if that 'wordSize' will be used too many times in the code?

In case it's the exactly the same speed at Run-time, which is better to use?

Old 10-07-2005, 02:59 AM   #4
Ankit mittal
LQ Newbie
Registered: Sep 2005
Location: india
Posts: 11

Rep: Reputation: 0
hello friend..

if u are concwern every nano sec of run time.

then i think #define is better way to write.
because it process by prepocessor and preprocessor put its value to all palces before any complition.
so at run time there is no need to substituting varible value.
i think my answer clarify u deference and help u.
Old 10-07-2005, 03:24 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 199Reputation: 199
if speed is so important why use C++?

I think you'll find it won't make a gnats of difference compared
with opening a file or printing a single character.

Make your code readable, simple and maintainable
that's good engineering.

Last edited by bigearsbilly; 10-07-2005 at 03:29 AM.
Old 10-07-2005, 11:43 AM   #6
LQ Newbie
Registered: Nov 2003
Location: NY area
Distribution: redhat EE3, Fedore Core 4
Posts: 23

Rep: Reputation: 15
good coding

don't use #define, it's not clean or typesafe

you can use an enum to represent integer values:

enum { myconst = 256 };

There should be no difference between the variable and the enum in performance IN SOME CASES (see below for how to optimize your code), unless you are doing things with pointers that the compiler does not understand:
const int x = 5;
int* p = (int*)&x; // you shouldn't do this anyway.

The compiler will panic and not to optimizations when you pull this kind of mischief.

If you have constant subexpressions:

double f(const double x, const double y) {
return x + y;

There is a world of a difference between this and:

inline double f(const double x, const double y) {
return x + y;

z= f(3,4)

Merely declaring them const means that they don't change in the program. inlining them shows the compiler that in fact, they are constants and can be folded.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
diff between #define and const defined variable b123coder Programming 7 06-27-2005 08:02 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
"int const" working problem. gpagedar Linux - General 0 10-08-2004 01:04 AM
global const int X = DEFINED (shows value in debugger?) abs Programming 2 09-25-2004 11:09 AM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration