Efficiency



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/03/2011 at 08:19, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:    
    Platform:      
    Language(s) :

    ---------
    Hi there,

    I'd like to write an article about efficient programming, but I need some things confirmed by MAXON or by people who know good what is happening internally in a computer.

    1. Global variables or struct ?
    Of course, global variables where they're not needed are senseless and inefficient.
    But what if I really need global variables ?
    And let's say: many of them.
    Is it better to create a struct or should I acces to a bunch of global variables ?

    2. Functions & variables
    Using functions does always need more capacity than reading out a variable.
    Even if I need a value given by a function only two times, it would be more efficient to create a variable for it. Is this statement right ?

    New questions coming soon 😂

    I would appreciate help
    -Niklas



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/03/2011 at 09:50, xxxxxxxx wrote:

    1. A singleton class is the best approach for 'global variables' in C++ and the Maxon SDK.  A struct is a C type which translates into a C++ public class so that would make them the same basically.

    2. Not sure that I understand.  Do you mean the difference between having a public variable in a class for access like myinstance->myvar; versus myinstance->GetMyVar(); or something else?  Whether or not to use public class variables or Get/Set methods for private class variables really depends upon the interface.  For you own code working within itself, public class variables are okay (not super-great, but okay).  For libraries, apis, sdks, anything where some 'third-party' has access to your code structures, you would prefer to use Get/Set methods.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/03/2011 at 10:58, xxxxxxxx wrote:

    I think overall from what I have seen it is inefficient to overuse Global variables.  I know that they definitely slow the system down.  A good general rule is to keep like data together so that the cpu does not have to search very far in to memory to find the data it needs.  As Robert mentioned, a struct is essentially the same thing as a class but it defaults to public.  So there is no comparision when talking abour global variables and structs.  I would go with the general rule that if a global variable is the only way to accomplish the task, then it must be used, otherwise use local variables.
     
    The use of access function like GetVariable or SetVariable are only needed if your variables are private and you do not want the user to have direct access to them.  I would say that depending on the situtaion as mentioned by Robert, you would need to use these accessor functions but it is probably less intensive (and I am not 100%) sure of this) to just directly access the variable.  Ultimately, efficiency comes down to memory management.  Creating your code in such a way that the least amount of memory is allocated at a given time.  Also making sure that you give back the memory when you are done with it to prevent memory leaks. 
     
    In the case of arrays, it is best to organize the data in a way that keeps all like data together.  You might benefit from looking at information about SoA (Structure of Arrays) or AoS (Array of Strctures).
     
    Anyway,  I hope that helps a bit.  I am no expert..  Not yet ..  LOL  but I like to think I am catching on.  :)
     
    ~Shawn



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/03/2011 at 15:17, xxxxxxxx wrote:

    Howdy,

    1. Well, I think Robert covered it.

    2. Yes, calling functions that return a value (or pointer or what ever) over and over within the same scope is much more costly than getting that value once and storing it in a local variable.

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/03/2011 at 08:33, xxxxxxxx wrote:

    Thanks to you all.

    1. Okei, so it is better to use a struct or did I missunderstand you ?

    2. Thanks Cactus this was exactly what i meant. :)

    Sorry, don't have much time to answer but thats good information for me.

    And what is C++ also applies to COFFEE or Python, apart from that COFFEE and Pythona re typeless, right ? The artivle should be in general, well for new C4D Developers but could be transferred to any language actually.

    Thanks, nux



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/03/2011 at 08:52, xxxxxxxx wrote:

    Yeah struct or class which are essentially the same thing other than the way they default.  :)


Log in to reply