Thread: Making function smaller and more efficient.

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    143

    Making function smaller and more efficient.

    I have this function here and I was wondering if anybody has any ideas on how to better it? I really need every bit of speed here and I know making it smaller will likely help that? Anybody got any ideas. BTW where I have comments is where I'll call some sample conversion function. Each case MUST have a different function call.

    Code:
    void processData(int index, int format, void *handler) {
    
    
    #ifdef LITTLE_ENDIAN
    
    
    	switch (format) {
    	case S_INT_8_LSB_1:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_8_MSB_1:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_8_NER_8:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_16_LSB:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_16_MSB:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_24_LSB:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_24_MSB:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_LSB_16:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_MSB_16:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_LSB_18:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_MSB_18:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_LSB_20:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_MSB_20:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_LSB_24:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_MSB_24:
    		Int32* data = new Int32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    		DataProcessor<Int32>* handler = (DataProcessor<Int32>*)handler;
    		handler->process(data, AudioStream::input->bufferSize);
    		break;
    	case S_INT_32_LSB:
    		Int64* data = new Int64[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case S_INT_32_MSB:
    		Int64* data = new Int64[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    		DataProcessor<Int64>* handler = (DataProcessor<Int64>*)handler;
    		handler->process(data, AudioStream::input->bufferSize);
    		break;
    	case FLOAT_32_LSB:
    		Float32 *data = new Float32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case FLOAT_32_MSB:
    		Float32 *data; new Float32[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    		DataProcessor<Float32>* handler = (DataProcessor<Float32>*)handler;
    		handler->process(data, AudioStream::input->bufferSize);
    		break;
    	case FLOAT_64_LSB:
    		Float64 *data = new Float64[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    	case FLOAT_64_MSB:
    		Float64 *data = new Float64[AudioStream::input->bufferSize];
    		// Convert and create convert back function pointer
    		DataProcessor<Float64>* handler = (DataProcessor<Float64>*)handler;
    		handler->process(data, AudioStream::input->bufferSize);
    		break;
    	}
    
    
    	// Call function pointer
    
    
    #else
    #error endianness not supported
    #endif
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does that even compile? I note that you are re-declaring data multiple times in the scope of the switch statement: the case labels and (presumably unshown) break statements do not create inner scopes. You should use an "extra" pair of braces immediately after the case label and before the break to create a block with that inner scope, or perhaps move each snippet of code to its own (inline) function (possibly declared in an unnamed namespace).
    Last edited by laserlight; 03-18-2015 at 10:32 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    It does compile. On both Visual Studio and GCC. But I'm a little confused on what you are saying. I know what I have above it BADDDD programming practice but I really don't have a choice.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I think I understand now. Perhaps you meant to write:
    Code:
    void processData(int index, int format, void *handler) {
    
    
    #ifdef LITTLE_ENDIAN
    
    
        switch (format) {
        case S_INT_8_LSB_1:
        case S_INT_8_MSB_1:
        case S_INT_8_NER_8:
        case S_INT_16_LSB:
        case S_INT_16_MSB:
        case S_INT_24_LSB:
        case S_INT_24_MSB:
        case S_INT_32_LSB_16:
        case S_INT_32_MSB_16:
        case S_INT_32_LSB_18:
        case S_INT_32_MSB_18:
        case S_INT_32_LSB_20:
        case S_INT_32_MSB_20:
        case S_INT_32_LSB_24:
        case S_INT_32_MSB_24:
            {
                Int32* data = new Int32[AudioStream::input->bufferSize];
                // Convert and create convert back function pointer
                DataProcessor<Int32>* handler = (DataProcessor<Int32>*)handler;
                handler->process(data, AudioStream::input->bufferSize);
            }
            break;
        case S_INT_32_LSB:
        case S_INT_32_MSB:
            {
                Int64* data = new Int64[AudioStream::input->bufferSize];
                // Convert and create convert back function pointer
                DataProcessor<Int64>* handler = (DataProcessor<Int64>*)handler;
                handler->process(data, AudioStream::input->bufferSize);
            }
            break;
        case FLOAT_32_LSB:
        case FLOAT_32_MSB:
            {
                Float32 *data; new Float32[AudioStream::input->bufferSize];
                // Convert and create convert back function pointer
                DataProcessor<Float32>* handler = (DataProcessor<Float32>*)handler;
                handler->process(data, AudioStream::input->bufferSize);
            }
            break;
        case FLOAT_64_LSB:
        case FLOAT_64_MSB:
            {
                Float64 *data = new Float64[AudioStream::input->bufferSize];
                // Convert and create convert back function pointer
                DataProcessor<Float64>* handler = (DataProcessor<Float64>*)handler;
                handler->process(data, AudioStream::input->bufferSize);
            }
            break;
        }
    
    
        // Call function pointer
    
    
    #else
    #error endianness not supported
    #endif
    }
    But what is with the hanging "Call function pointer" comment?

    EDIT:
    Quote Originally Posted by cmajor28
    It does compile. On both Visual Studio and GCC. But I'm a little confused on what you are saying. I know what I have above it BADDDD programming practice but I really don't have a choice.
    Really? Is that actually your code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    It is. I had your code before but it actually didn't compile on Visual Studio. Surprised me a lot because my old code/your code is ALOT prettier and easier to read.
    One thing different about mine is in each case statement I have the comment. Each SEPERATE case has a different conversion function.

    And that comment means I need to call a function to convert the samples back and then but it back into the correct buffer.

    EDIT: But I see what you mean about the scope thing now.
    Last edited by cmajor28; 03-18-2015 at 10:40 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cmajor28
    It is. I had your code before but it actually didn't compile on Visual Studio. Surprised me a lot because my old code/your code is ALOT prettier and easier to read.
    Including the "extra" braces?

    Out of curiosity, but does this program compile for you?
    Code:
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
        switch (argc)
        {
        case 1:
            {
                int n = 1;
        case 2:
            int n = 2;
        case 3:
            int n = 3;
            std::cout << n << std::endl;
            break;
        }
    }
    EDIT:
    Quote Originally Posted by cmajor28
    One thing different about mine is in each case statement I have the comment. Each SEPERATE case has a different conversion function.
    Sigh. Then please either post your actual code, or make it obvious that there is code that is omitted, e.g., include a "// ..." comment along with the break; statement that is actually there and which affects the logic.

    Quote Originally Posted by cmajor28
    And that comment means I need to call a function to convert the samples back and then but it back into the correct buffer.
    Have you actually written that code? What variables are involved?
    Last edited by laserlight; 03-18-2015 at 10:46 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    It does not. But I have NO IDEA why not because my above code compiles.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, sorry, I made a typo. Try:
    Code:
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
        switch (argc)
        {
        case 1:
            int n = 1;
        case 2:
            int n = 2;
        case 3:
            int n = 3;
            std::cout << n << std::endl;
            break;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    Yeah is does.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Even in g++? What version of MSVC and g++ are you using?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    1800 MSVC. I didn't try it on GCC. It may have to deal with my compiler settings. For this project they are changed ALOT because of some fixes I had to make for an SDK I'm using.

    Edit: Looking at the code I have NO IDEA how it compiles for me. Maybe I have some errors disabled because the program acts weird when it runs. I put some gotos and it all worked perfect.
    Last edited by cmajor28; 03-18-2015 at 10:57 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cmajor28
    1800 MSVC. I didn't try it on GCC. It may have to deal with my compiler settings. For this project they are changed ALOT because of some fixes I had to make for an SDK I'm using.
    I don't have MSVC on this computer, but checking with this purported online version of MSVC shows that it does give the error messages that I expect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    I think I may have a macro defined that disables that error message. Cause the program doesn't really run at all.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cmajor28
    Edit: Looking at the code I have NO IDEA how it compiles for me. Maybe I have some errors disabled because the program acts weird when it runs. I put some gotos and it all worked perfect.
    Err... that's horrible. I know we do joke about code that works and we don't know why, and in reality that does happen especially when we take over maintenance of a code base for which the previous developer(s) are no longer available for consultation, but you do need to know why the code you just wrote works, otherwise you're programming blind. Putting in some gotos (which should not be done in the first place without very, very good reason in C++) and seeing that it "all worked perfect" is just asking for trouble: in such a case I dare not suggest anything to you because my suggestions could well break the code that nobody understands, and in fact even a change of say, optimisation level could well cause such brittle code to break.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    I agree. The gotos are really dirty. I have a better fix on the way though. Give me a few minutes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making an efficient tiling engine
    By HelpfulPerson in forum Game Programming
    Replies: 2
    Last Post: 12-11-2013, 05:13 PM
  2. Suggestions for making my code more efficient?
    By HelpfulPerson in forum C Programming
    Replies: 27
    Last Post: 08-13-2013, 12:29 PM
  3. More efficient way to structure this function?
    By Justin H in forum C Programming
    Replies: 8
    Last Post: 10-20-2012, 12:28 AM
  4. New Smaller and Smaller Arrays.
    By jastiv in forum Game Programming
    Replies: 1
    Last Post: 07-02-2012, 01:37 PM
  5. more efficient prime function??
    By codingGuy in forum C Programming
    Replies: 10
    Last Post: 10-12-2011, 05:09 PM