Thread: Sprintf and sscanf functions slow

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Sprintf and sscanf functions slow

    I am using Borland C++ 4.0 for building a embedded module. Module is required to do very time critical tasks. Code is already developed using functions like sscanf and sprintf. And I heard from my colleague that these functions are comparatively slow. Is this true? And if yes, what is alternative routine I can use?

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    What you mean slow? how many slow? comparing to what? never heard that before.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Slow means takes long time to finish its task.

    Also want to know, whether they really take longer time to execute. If yes, is there any alternative.

    Let me know

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And I heard from my colleague that these functions are comparatively slow.
    Compared to what? Generally, the standard library functions will be sufficiently fast to suit your needs. Try profiling the software to see if it remains within acceptable performance limits. Only then should you consider optimizing.

    >Also want to know, whether they really take longer time to execute.
    Longer than what? This is a broad question. sscanf most definitely takes longer than say, integer addition. But for a more realistic comparison, sscanf and the equivalent calls to string handling and conversion functions, you should only worry about it if there is a signifigant performance bottleneck in that area.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I am trying to strip off the number from ASCII string and convert to hex. So previously written code use sscanf for doing this. Now, I want similar operation at other part of code. So if I use sscanf, which helps compatibility and achieves what we want. But if it is too slow, I want to use atoi function.

    And I am not sure, atoi or atoh(hex) function does range check of return value after conversion, So I may develop customized my_atoi function.

    So wonder, how worth to write our own atoi or use atoi routine instead of sscanf?

    Please let me know.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    sscanf is slower than doing a direct conversion because it's a more generic tool. It may be 2 or 3 times slower (or more, or less), depending on your implementation. Profile the code. If you meet the realtime demands, don't worry about it. If you don't then find out where you don't and work out ways in which it can be made faster.

    Unless you're calling sscanf/sprintf many many times then changing it to something even 10 times faster will make little difference.

    If you do want to change them, I'd suggest using strtol, stroul and friends which do provide range checking.

    Ian Woods

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    What about sprintf? This is also going to be slow as well.
    Any function that are faster than this.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What about sprintf?
    The same comments hold.

    >Any function that are faster than this.
    Yes, but you seem to be ignoring the point.

    -Prelude
    My best code is written with the delete key.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>atoh(hex)
    Try strtol()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    sscanf is slow but can be sped up significantly

    Quote Originally Posted by Roaring_Tiger View Post
    I am using Borland C++ 4.0 for building a embedded module. Module is required to do very time critical tasks. Code is already developed using functions like sscanf and sprintf. And I heard from my colleague that these functions are comparatively slow. Is this true? And if yes, what is alternative routine I can use?
    I have faced with this issue myself recently. My program has to process the string consisting of two columns: words and the document ids where those words are located. The string was of the following format:

    word1 docID1
    word1 docID1
    word1 docID2
    word2 docID1
    ........................
    wordi docIDj

    In my program I had to create an inverted index (list of ints representing all documents where a certain word is present - in example, for word1, docID1 and docID2 would be the contents of the index). Also, in case the same word is met in the same document, the frequency must be populated

    I have decided to use sscanf function on the string in the following way:
    Code:
    while ( sscanf(bigString, "%s %d", tempWord, tempDocID)  )
    {
         // if the size of the current line is 0, end of string is reached
    	if ( (lineSize = currentLineLength(subArray)) == 0 ) break;
       
       < Process the obtained string and integer >
      
       bigString = bigString + lineSize;  // iterate through the string to scan the next row
    }
    The size of bigString was more than a million characters. As a result, processing it took 3 minute on average. I have modified the code to split the bigString into small 20K strings and process all of them. The following modifications were made:
    Code:
    while ( bigString[bigStringCntr + smallStringCntr] != '\0' )
    	{
    		smallString = malloc(20000*sizeof(char));
                    // copy the data from bigString to smallString
    		while ( smallStringCntr <= 20000 )
    		{
    			smallString[smallStringCntr] = bigString[bigStringCntr + smallStringCntr];
    			if ( (smallStringCntr > 15000 && smallString[smallStringCntr] == '\n') || smallString[smallStringCntr] == '\0' ) break;
    			smallStringCntr++;
    		}
    		smallString[smallStringCntr] = '\0';
    
                   while ( sscanf(smallString, "%s %d", tempWord, tempDocID)  )
                   {
                       // if the size of the current line is 0, end of string is reached
    	          if ( (lineSize = currentLineLength(smallString)) == 0 ) break;
       
                      < Process the obtained string and integer >
      
                     smallString = smallString + lineSize;  // iterate through the string to scan the next row
                    }
    
                   bigStringCntr = bigStringCntr + smallStringCntr; // iterate through the big string to process the next 20K small string
          }
    The code has many details missing but that's not important. The point is - processing sscanf on many small strings rather than one large string is way more efficient. For example, from 3 minutes per 1 - 2 mbs of data processing time went down to less than a second to process the same exact data but in a smaller substrings. This has been a great discovery for me and a great leap forward for my project
    Last edited by mityay; 03-22-2009 at 12:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  2. Dumb Question: What is sscanf?
    By KingZoolerius66 in forum C Programming
    Replies: 3
    Last Post: 10-04-2003, 08:19 PM