More efficient: pointer to structures data types or dynamic

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

More efficient: pointer to structures data types or dynamic

Post by sg57 »

Heres a quick question...

Are pointers to structures data types more efficient (AKA faster, less memory, etc.) rather then having a direct/dynamic call to the data type?

In lamen terms:

screen.x

or

screen->x

What is more efficient?
PeterM
Posts: 125
Joined: Sat Dec 31, 2005 7:25 pm
Location: Edinburgh, UK
Contact:

Post by PeterM »

screen->x will be slower than screen.x due to the pointer indirection.

HOWEVER, you probably don't want to pass large structures around by value as function arguments just so you can skip some indirection in the function.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

uhm, i wouldnt be sure about this....in many cases the generated assembly will be the same.
PeterM
Posts: 125
Joined: Sat Dec 31, 2005 7:25 pm
Location: Edinburgh, UK
Contact:

Post by PeterM »

That's if the screen pointer is already in a register though, right?
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

in many (most) cases the compiler will load the base of the struct into a register and use that to access various members of the struct within a function. you have to keep in mind that even if the struct is a global static (which means its memory adress is known at compile time) can not be simply accessed without "pointer indirection". (there is no immediate load from a 32bit adress). also if the struct is local in a function, it will be created on the stack and thus the (a) pointer that can be used to access it IS already in a register. so while it does make a difference on other cpus, on mips (and other risc processors) the resulting code would be almost the same.[/code]
PeterM
Posts: 125
Joined: Sat Dec 31, 2005 7:25 pm
Location: Edinburgh, UK
Contact:

Post by PeterM »

Neat.
ufoz
Posts: 86
Joined: Thu Nov 10, 2005 2:36 am
Location: Tokyo
Contact:

Post by ufoz »

More importantly, if this is the sort of optimization you're worried about, chances are you're wasting your time...
Post Reply