sscanf ~1000 slower that strtol

Florian Weimer fweimer@redhat.com
Wed Jun 1 18:47:00 GMT 2016


On 06/01/2016 07:50 PM, Michael Williamson wrote:

> I have a question. I was comparing the speed of reading some ASCII
> integers by different methods, and I found that using sscanf (with the
> characters consumed operand (%n)) is ~1000
> slower than the fastest method, strtol. Why is it so slow?

Each invocation of sscanf counts the number of characters in the input 
string.  For more typical use cases, doing it this way is faster than 
checking for the null terminator while parsing the input string.  In 
your scenario, this leads to very bad performance.

You could use fmemopen and fscanf to avoid this problem.

Florian



More information about the Libc-help mailing list