Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Constants for numerics in DB / C?

Author
Message
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Jul 2004 19:50
I just want to know if this would apply in DarkBasic (or C, as I guess that's the lower common denominator for DarkBasic)...

In my weird and wonderful world of Midrange programming, we often replace numbers 1...10 with constants. Why? Because if you use the number "1" a hundred times in your program, you are actually creating 100 temporary variables to store the value in. So it's more efficient to create one variable manually yourself, and refer to this instead of hardcoding the value.

Does this apply in C, and therefore DarkBasic?

The real question I suppose, is whether or not to replace commonly used values with constants for a more efficient program?

BatVink
http://biglaugh.co.uk/catalog AMD 3000+ Barton, 512Mb Ram, 120 Gig Drive space, GeForce 5200 FX 128 Mb, Asus A7N8X Mobo.
Terms & Conditions apply
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 19th Jul 2004 20:20 Edited at: 19th Jul 2004 20:21
Quote: "you are actually creating 100 temporary variables to store the value in"

Eh ? If you type in a number 100 times you wouldn't be creating any temporary variables (because its a number).

Quote: "The real question I suppose, is whether or not to replace commonly used values with constants for a more efficient program?"

Only if they would be used more than once.


Are you sensitive enough?
jasuk70
21
Years of Service
User Offline
Joined: 3rd Dec 2002
Location: Hemel Hempstead
Posted: 19th Jul 2004 20:31 Edited at: 19th Jul 2004 20:31
The criteria I use for constants is to make development easier by having only one point of change, and make reading the code much easier by replacing numbers with meaningful text.

Example of the first is say the maximum number of characters which is used in loops through out the code

#CONSTANT MaxCharacter 6

FOR Lp=1 TO MaxCharacters

An example of the second is making things like images have their number replaced by text

#CONSTANT CancelButtonImage 1
#CONSTANT SaveExitButtonImage 2
#CONSTANT NextButtonImage 3

PASTE IMAGE CancelButton 10,10

This makes debugging and reading the code much easier.

Jas

----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"
Killswitch
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location: School damnit!! Let me go!! PLEASE!!!
Posted: 19th Jul 2004 20:33
V. off topic but love your sig!

~It's a common mistake to make, the rules of the English langauge do not apply to insanity~
[IMG][/IMG]
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Jul 2004 20:44
TCA...that's my point, I think. On the midrange machines I work on, you are indeed creating 100 temporary variables, each one taking up it's own little slot in memory.

So in a 28,000 line program, it's very important to create a "constant" variable for frequently used options. Especially when you consider you could have 2,000 people working on one server.

So can I assume your answer is "no", this is not a consideration in C-based languages/engines?

Jasuk: Yes, I'm with you on that point. Being from a structured language background, I take it one step further and put most of these in a parameter file. Then you can edit the file and not have to recompile when you make a change. There's a little more work involved in the initialisation, but worth it in the long run.

BatVink
http://biglaugh.co.uk/catalog AMD 3000+ Barton, 512Mb Ram, 120 Gig Drive space, GeForce 5200 FX 128 Mb, Asus A7N8X Mobo.
Terms & Conditions apply
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 19th Jul 2004 21:07
Quote: "So can I assume your answer is "no", this is not a consideration in C-based languages/engines?"

It depends - like any value, if its going to be used more than once, then put it in a variable.

I've never know C to create temporary variables when using numeric calculations - it should convert the number use it directly (or perhaps put it in a register).


Are you sensitive enough?
jasuk70
21
Years of Service
User Offline
Joined: 3rd Dec 2002
Location: Hemel Hempstead
Posted: 19th Jul 2004 21:33
Batvink: Great minds . I've got quite a few options in external files for exactly the same reason and to allow testers to tweek features etc. I'm up to 4300 lines now and compilation is starting to become a bit of a pain. I'm getting to the point too where I'm starting to think about re-organising variables to save on memory required.

Killswitch: Being a QA person it seems too close to the truth

Jas

----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Jul 2004 00:10
Jasuk: I have an include file that reads in the variables and puts them in a 2D array. There are then more routines that allow you to query the values, and return the value in the correct format (character, single, integer etc). Works well for me, as I do this outside of any time-critical functions.

However. I'm now at the stage where I think TCA's INI file plugin would be better as it will allow me to edit and write the values back.

TCA: Will your DLL allow me to read the values without knowing the keywords, or will I need to do this manually?

BatVink
http://biglaugh.co.uk/catalog AMD 3000+ Barton, 512Mb Ram, 120 Gig Drive space, GeForce 5200 FX 128 Mb, Asus A7N8X Mobo.
Terms & Conditions apply
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 20th Jul 2004 00:34
You would need the keyword and section name - if either isn't given then usually it would copy everything in the section name (or the whole thing) - at least according to the API documentation. Unfortunately its never worked out for me like that (could never get anything meaningful back).


Are you sensitive enough?
jasuk70
21
Years of Service
User Offline
Joined: 3rd Dec 2002
Location: Hemel Hempstead
Posted: 20th Jul 2004 00:57
Using the undocumented features that arrived in 5.5 (GET FIRST TOKEN and GET NEXT TOKEN), I've knocked up a quick routine to handle .ini style of thing. So my config files look like:



and reference it with

Int=GetConfigInt("main.screen.width")

Unfortunatly as with the other string things in DBP if the file gets big it starts to get really slow and unusable. But for now it will do me.

Jas

----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"
GothOtaku
20
Years of Service
User Offline
Joined: 23rd Nov 2003
Location: Amherst, MA, USA
Posted: 20th Jul 2004 04:23
In regards to constants versus literal numbers in code, it depends on your compiler and how and how well it's optimized although sometimes your processor can play a hand in it. As for DB, I have no idea, try some tests and see what happens.

Login to post a reply

Server time is: 2024-09-22 06:44:59
Your offset time is: 2024-09-22 06:44:59