Results 1 to 5 of 5

Thread: Usefulness of static allocation ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Usefulness of static allocation ?

    Hi,

    This question is about speeding up things when performing repetitive calculations such as drawing on a widget.

    Will it be faster if the local variables are allocated statically rather than on the heap ?
    What about QStrings : since their size can vary, is there any point to declaring them as 'static' ?

    I tried both and can't see much difference, but I can't measure it precisely either

    Thanks for any insight in these matters

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: Usefulness of static allocation ?

    maybe static is a tiny bit faster, you won't notice that, though.

    The key is not to do a faster recalculation, but to avoid it if it is not necessary (i.e. cache your result(s))
    and only recalculate when and what you need to.

    Did you use a profiler to detect your bottleneck? Otherwise I would not think about stuff
    like static vs heap allocation.

  3. The following user says thank you to caduel for this useful post:

    Windsoarer (28th February 2010)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Usefulness of static allocation ?

    Well, if the objects are static, they only get created once, but the overall answer to your question depends on how many times they are created otherwise (are they in a tight loop for example?)

    My advice would be to not do premature optimisation. Profile your code, find the slow parts and optimise those. Ignore all other code, even if it looks slow. The time spent on such stuff normally isn't worth the payback, and you risk introducing bugs.

  5. The following user says thank you to squidge for this useful post:

    Windsoarer (28th February 2010)

  6. #4
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Usefulness of static allocation ?

    OK thanks - I didn't know about 'profilers".

    Any advice on the one to use ?

  7. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Usefulness of static allocation ?

    Well, that depends on your compiler, OS, and processor. Visual Studio comes with a profiler, as does GCC. If you use an AMD processor, then AMD do a very good profiler which can go into great depth. Intel do a similar profiler for Intel processors, but the AMD one was free last time I checked.

    If your using GCC and don't mind command lines, have a look at 'gprof'. Not as good as the processor-specific ones, but it might be enough, depending on the level of profiling you want.

    Have a look here: http://linuxgazette.net/100/vinayak.html

Similar Threads

  1. Virtual memory allocation problem
    By morfei in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2009, 11:30
  2. QDrag : memory allocation
    By kghose in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2008, 22:57
  3. Vector allocation
    By ToddAtWSU in forum General Programming
    Replies: 4
    Last Post: 22nd June 2007, 21:37
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 09:21
  5. vector memory allocation
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 23rd March 2006, 17:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.