Fragger's stupid questions, chapter 1.

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
mankrip
DCEmu Ex-Mod
DCEmu Ex-Mod
Posts: 3712
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sun Nov 04, 2001 5:12 pm
Has thanked: 0
Been thanked: 0
Contact:

Fragger's stupid questions, chapter 1.

Post by mankrip »

For some reason I always forget to ask things here... but luckly I remembered to do it now.

- Is there any difference between > and >=, speed-wise? For example:

Code: Select all

if (myvar > 1) somefunction();

Code: Select all

if (myvar >= 2) somefunction();
Just it for now, I'm busy with other things and don't remember what else I want to ask.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Image
User avatar
semicolo
Mental DCEmu
Mental DCEmu
Posts: 328
Joined: Mon Apr 25, 2005 1:02 pm
Location: Three-rivers canada
Has thanked: 0
Been thanked: 0
Contact:

Post by semicolo »

Well it may depend on the instruction set of the processor you're using, on x86 processors there are instructions for testing > or >= with the same execution cycle numbers.
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Post by nymus »

Another factor is the size of the immediate value eg the sh4 can only compare -127-128 so if you said >=128, the compiler (or you) would have to hack a little bit to avoid storing 128 in memory.

Such worries are not as important as a clear program. Most programs I've seen use the > notation so you should use that.
behold the mind
inspired by Dreamcast
Ex-Cyber
DCEmu User with No Life
DCEmu User with No Life
Posts: 3641
Joined: Sat Feb 16, 2002 1:55 pm
Has thanked: 0
Been thanked: 0

Post by Ex-Cyber »

-127-128
Shouldn't that be -128 - 127?
"You know, I have a great, wonderful, really original method of teaching antitrust law, and it kept 80 percent of the students awake. They learned things. It was fabulous." -- Justice Stephen Breyer
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Post by nymus »

True.
behold the mind
inspired by Dreamcast
User avatar
mankrip
DCEmu Ex-Mod
DCEmu Ex-Mod
Posts: 3712
Joined: Sun Nov 04, 2001 5:12 pm
Has thanked: 0
Been thanked: 0
Contact:

Post by mankrip »

Is there any speed difference between calling a function and using the code "directly"?
Well it may depend on the instruction set of the processor you're using, on x86 processors there are instructions for testing > or >= with the same execution cycle numbers.
Thanks.
Another factor is the size of the immediate value eg the sh4 can only compare -128-127 so if you said >=128, the compiler (or you) would have to hack a little bit to avoid storing 128 in memory.
Do you mean 128 bits (FFFF FFFF hex, 4 294 967 295 dec)?
Such worries are not as important as a clear program. Most programs I've seen use the > notation so you should use that.
Thanks. It's just that I tend to be nitpick when I'm working :wink:.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Image
doragasu
DCEmu Cool Poster
DCEmu Cool Poster
Posts: 1048
Joined: Thu May 16, 2002 5:01 pm
Location: Madrid, Spain
Has thanked: 0
Been thanked: 0

Post by doragasu »

Fragger wrote:Is there any speed difference between calling a function and using the code "directly"?
Using the code "directly" is faster, but there's a keyword -inline- you can use before a funcion declaration for the compiler to put the code of the function "directly" where the call is, and also when you enable optimization options, compilers auto-inline functions they consider. For extremely simple things, you can also use macros instead of functions.
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Post by nymus »

doragasu wrote:
Fragger wrote: For extremely simple things, you can also use macros instead of functions.
Most programmers today frown on the use of using macros in working/executable code. Leave macros for configuration.
behold the mind
inspired by Dreamcast
doragasu
DCEmu Cool Poster
DCEmu Cool Poster
Posts: 1048
Joined: Thu May 16, 2002 5:01 pm
Location: Madrid, Spain
Has thanked: 0
Been thanked: 0

Post by doragasu »

nymus wrote:
doragasu wrote:
Fragger wrote: For extremely simple things, you can also use macros instead of functions.
Most programmers today frown on the use of using macros in working/executable code. Leave macros for configuration.
I think simple macros are still pretty useful when coding for systems with limited resources (like DC, consoles like GBA, embedded systems, etc.). It's true you have to be very careful when working with macros (put all the input parameters in brackets, do all kind of castings, etc) but it it's fast, easy and works, why shouldn't I use it?
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

I tend to use inline functions (or methods, depending on context) when writing C++, and a mix of macros and inline functions in C, usually macros only. Inline functions aren't really as good in C as they are in C++ so macros are an acceptable alternative, provided you're careful with them (put brackets around all paramaters in the macro itself, and use each parameter once and once only to avoid unpredictable side effects). Still a little clumsy, but not too bad.

They have the added advantage that they can be (kind-of) polymorphic, so you could have a single macro that can work on multiple types, while still remaining as type safe as C ever is (which is to say, not very, but neither is C++). You can do that in C++ using either multiple functions / methods with the same name but different signatures (if you don't mind re-defining the same thing for every conceivable data type), or using templates (which is far less transparent to use). Hmm... You can tell I've been doing work with functional programming languages, can't you?

Admittedly, I wouldn't really want to use macros in C++ or Java (they're not necessary there), but for lower level languages, why not?
doragasu
DCEmu Cool Poster
DCEmu Cool Poster
Posts: 1048
Joined: Thu May 16, 2002 5:01 pm
Location: Madrid, Spain
Has thanked: 0
Been thanked: 0

Post by doragasu »

BlackAura wrote:Admittedly, I wouldn't really want to use macros in C++ or Java (they're not necessary there), but for lower level languages, why not?
I didn't know you can use macros in Java... How does a Java macro look like? (just curious).
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

I didn't know you can use macros in Java... How does a Java macro look like? (just curious).
Actually, you can't. But you wouldn't really want to anyway. Possibly didn't make myself clear there...
Moi
QuakeDev Respected
QuakeDev Respected
Posts: 592
Joined: Thu Mar 14, 2002 1:15 am
Has thanked: 0
Been thanked: 6 times

Post by Moi »

I know this topic is quite old but i think this link is quite interesting:

http://www.custard.org/~andrew/optimize.php
Create your own Dreamcast games using the Quake-Engine:
http://quakedev.dcemulation.org/develop/getstarted.htm
User avatar
toastman
Iron Fist of Justice
Iron Fist of Justice
Posts: 4933
Joined: Sat Nov 10, 2001 3:08 am
Location: New Orleans
Has thanked: 0
Been thanked: 0
Contact:

Post by toastman »

Fragger wrote:Is there any speed difference between calling a function and using the code "directly"?
I just want to chime in here on this because while your question was answered, I felt the "why" was left out.

Using a macro, the inline keyword, or just retyping the code whereever you need it is faster than using a function because calling a function creates a certain amount of overhead.
Normally, code executes in a linear fashion. Each instruction, one after the other. Certain instructions can jump to different locations in the code and begin execution there.
This essentially (and this is very basic) how functions work. When you call your function, memory is allocated, values are pushed on the stack, and the jump is made to your function. Your function executes, then everything happens in reverse and you may return something.
With macros, "inlining" the function, or retyping, you aren't creating that overhead. A macro or inline function replaces the function call before your code actually is processed by the compiler. Retyping the function is avoiding the issue all together and is just a manually doing what the macro and inline already do.
So yes, your code will run faster if you inline or macro your functions, however, your executable will be larger as all of those function calls will be replaced by actual code.
If space is not an issue, and you need every bit of speed you can get, then inline/macro away. However, on low memory systems, it might be wise to use functions to reduce on your program's footprint.
And the question is also, how much speed will you be gaining by doing this? Is the speed worth the larger footprint?
No signature.
Post Reply