Static vars

This is a discussion on Static vars within the C++ Programming forums, part of the General Programming Boards category; Say I have a function runs a coupla 1000 times. Would it increase the speed if i declared the variables ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    17

    Static vars

    Say I have a function runs a coupla 1000 times.
    Would it increase the speed if i declared the variables in that function as static and simply re-initialized their values myself every time?

    I mean, it would avoid having to allocate/deallocate the variables every time the function runs... or perhaps I have the wrong idea of how static variables work??

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    First thing to bear in mind is that local variables might be completely optimised out of existence (and placed in registers) by the compiler. This cannot happen if you make your variables static.

    Also, unless your function is really simple (compiler dependent), you are going to get a stack frame, and its usually one instuction to set this up, no matter the number of local vars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21