LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hi!

If SPEED is the most important thing in a program,

which is faster

#define wordSize 256
OR
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
mailelam
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
Hady
Member
 
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?

Thanks!
 
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
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
dovkruger
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 PM.

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