Global Variable vs Struct Variable Question

From: Bryan Parkoff (spamtrap_at_crayne.org)
Date: 03/28/05


Date: Mon, 28 Mar 2005 07:39:08 +0000 (UTC)


    I have read Pentium IV Optimization manual that it explains to avoid
global variable because it is slow. MOV instruction has to direct memory
address to retrieve global variable, but it does not have pointer. It makes
sense that global variable has its own memory address individually.
Individual memory address does not store in the cache.
    Now, some local variables are stored in struct that struct always have
pointer. MOV instruction has to direct pointer once before it begins to
indirect memory address to retrieve local variable through pointer like
struct. Pointer does store in the cache because pointer is used again and
again.
    My question is -- struct and class in C++ are useful for local
variables, but it is a very bad idea to bind member functions through
pointer. Member functions inside struct or class can slow and degrade the
performance on Pentium IV. It is preferable to use global functions outside
of struct or class while global functions can still access local variable
inside struct or class.
    My test with timing stamp shows that member function inside struct and
class are slower than global functions. How can you please explain why it
happens? Why do programmers encourage to use member functions through
pointer because they claim that member functions can be stored in the cache
for better performance?
    It is very important to me that I need to know.

Bryan Parkoff



Relevant Pages

  • Re: Looking for a more elegant way to do memory offsets
    ... The mallocfunction returns memory that is "suitably aligned" ... >which in it contains the pointer to the first element in the linked ... and here a "struct dem_mem" does not contain a pointer to another ... This is not a linked-list access, but rather an array access: ...
    (comp.lang.c)
  • Re: C Socket Programming question
    ... All I really need to make the socket connection is ... But over-writing random memory can be so much fun. ... > You still have to allocate memory and save the pointer to .h_addr. ... > for struct hostent *tmp as well as the space for its members ...
    (comp.unix.programmer)
  • Re: Objects
    ... i know that an object is a region of memory that can represent values. ... The struct becomes an object once it's defined. ... If you read my post you will find that I have described struct type ... after the pointer to which its address was assigned. ...
    (comp.lang.c)
  • Re: Function pointers (Callback functions) in Java ?
    ... > a struct, into a heavy, clumsy object. ... > around in memory is not a pleasing prospect. ... Lets compare a C struct to a Java object. ... The Java object typically has one additional field, a pointer ...
    (comp.lang.java.programmer)
  • Re: Global Variable vs Struct Variable Question
    ... > address to retrieve global variable, but it does not have pointer. ... some local variables are stored in struct that struct always have ... MOV instruction has to direct pointer once before it begins to ... Member functions inside struct or class can slow and degrade the ...
    (comp.lang.asm.x86)