Will 'static' optimise my code?

This is a discussion on Will 'static' optimise my code? within the C++ Programming forums, part of the General Programming Boards category; Code: void samRing::getParameterDisplay (long index, char *text) { static char waveName [4] [4] = { "Sin", "Tri", "Squ", "Saw" }; ...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    Will 'static' optimise my code?

    Code:
    void samRing::getParameterDisplay (long index, char *text)
    {
    	static char waveName [4] [4] = { "Sin", "Tri", "Squ", "Saw" };
    
    	switch (index)
    	{
    		case pFreq:
    			sprintf (text, "%d", (int) (freq * MAX_FREQ));
    		break;
    
    		case pWaveType:
    			strcpy (text, waveName [waveType]);
    		break;
    
    		case pDry:
    			sprintf (text, "%d", (int) (dry * 100));
    		break;
    	}
    }
    I'm presuming that because the array is static, the assignments will only occur once. In all subsequent calls, this will be skipped over. Am I right?

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Yes, after waveName is initialized the first time the function is called, subsequent calls won't reassign it. But any speed gain will probably be negligible.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Make it a constant (const char WaveName[] ...) and the compiler *might* optimize it even further.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I wasn't hoping to make great gains in speed, I just didn't like the thought of that array being re-assigned every call - it's just sloppy. But thanks for the const idea, I'll do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a static library from C++ and C code
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2009, 08:28 PM
  2. tools for static code analysis
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2009, 01:41 PM
  3. error with Static Library code
    By ghostshadow189 in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2007, 04:12 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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