Thread: Repeated calls to sscanf

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    Repeated calls to sscanf

    Code:
    int convert_data(int id_limb){
    	char temp1[VECTOR_SIZE];
    	char temp2[VECTOR_SIZE];
    	char code;
    	char code_temp;
    	double velocities[8];
    	int i;
    
    	
    	for (i=0;i<6;i++)	//initializing
    	{
    		memcpy(temp1,buffer+i*VECTOR_SIZE,VECTOR_SIZE);
    		sscanf(temp1,"%s",temp2);
    		velocities[i]=atof(temp2);  //
    	}
    	memcpy(&codigo_temp,buffer+6*VECTOR_SIZE,1);
    	sscanf(&codigo_temp,"%c",&code);
    	printf("\nCode %c \n",code);
    	
    return(execute_comand(velocities, code, id_limb));
    
    }
    The overall project consist of an operator sending linear and angular velocities to a robotic arm as a string of chars through the lan. As of now we need to speed up the process (it's about 0.5 ms too slow). Is there any way to optimize this particular function? For example, a way to skip the repeated calls to sscanf?

    Thanks a lot

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    buffer is global? You don't seem to initialise temp2.
    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
    Jun 2005
    Posts
    6,815
    Yeah. Eliminate the temp1 buffer and also the sscanf calls. Neither are necessary. Also eliminate the printf() call, except when debugging.

    When using sscanf, the %s format scans until it encounters either a whitespace or the end of the input string. As long as you guarantee you do not change buffer, you can do the same using a loop. Similarly, %c simply gets the next character. If all you're reading from the string is a single character, you are getting the first character.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Quote Originally Posted by grumpy View Post
    Yeah. Eliminate the temp1 buffer and also the sscanf calls. Neither are necessary. Also eliminate the printf() call, except when debugging.

    When using sscanf, the %s format scans until it encounters either a whitespace or the end of the input string. As long as you guarantee you do not change buffer, you can do the same using a loop. Similarly, %c simply gets the next character. If all you're reading from the string is a single character, you are getting the first character.

    I'm not sure I follow, could you please elaborate a bit more? Also, the string consist of 6 different characters at all times (3 linear components and 3 angular).

    @laserlight buffer is global. As for why I don't initialize temp2, I figured I didn't need to since scanf would write there anyway

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I just described what sscanf() does with the %s and %c format specifiers. It is not hard to replicate that behaviour. And sscanf() involves an overhead (interpreting the format string, etc) that you can avoid with a bit of thought.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If it's just a space separated list of numbers, then use this
    man page strtod section 3

    Note the last parameter, you use this to "walk" along the string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    I was under the impression that memcpy simply copied the memory blocks, but but didn't format the data. Therefore I used sscanf to format them as strings, was my logic wrong?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you showed us what buffer contains, then we might be able to suggest a better way.

    Unless you have say "123456" and you expect to get 123 and 456 as separate numbers, you seem to be doing a lot of extra work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    char buffer[BUFSIZE]

    As of right now there are seven different elements in the string: x,y,z linear and x,y,z angular. There's an extra character E,C or H that tells the machine to exit the program, continue or go home. So yes, the remote machine should be able to tell them apart.

    Thanks everyone for the help

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    I'm goig to try
    Code:
    for (i=0;i<6;i++)    //inicializacion
    
        {
            strncpy(temp1,buffer+i*VECTOR_SIZE,VECTOR_SIZE);
            velocities[i]=atof(temp1);
         
    }
          strncopy(&codigo,buffer+6*VECTOR_SIZE,1);
    will report results and speed later

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But strncpy doesn't append a \0 to where you copy it from.
    Like I said, so long as you don't have contiguous numbers (there is some kind of space, tab, comma, alpha separator), then you can just do this instead.

    velocities[i]=atof(buffer+i*VECTOR_SIZE);

    atof will stop converting as soon as it comes across something which isn't a digit or a decimal point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Repeated addition
    By BB89 in forum C++ Programming
    Replies: 22
    Last Post: 02-14-2010, 04:01 PM
  2. Repeated Addition
    By rocksteady in forum C Programming
    Replies: 15
    Last Post: 12-11-2007, 07:05 AM
  3. how to: Non repeated random #s
    By ypramesh in forum C Programming
    Replies: 2
    Last Post: 03-08-2006, 05:15 PM
  4. repeated random
    By xdeath in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2003, 03:52 PM
  5. repeated value in file
    By lockpatrick in forum C Programming
    Replies: 3
    Last Post: 05-11-2002, 11:57 PM

Tags for this Thread