This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: sscanf ~1000 slower that strtol
- From: Florian Weimer <fweimer at redhat dot com>
- To: Michael Williamson <michael dot h dot williamson at gmail dot com>
- Cc: libc-help at sourceware dot org
- Date: Wed, 1 Jun 2016 20:47:49 +0200
- Subject: Re: sscanf ~1000 slower that strtol
- Authentication-results: sourceware.org; auth=none
- References: <CAKZygbcyX4kygLSq_BM9uS7HZYQpyOo6TQQwvLip2QHd-ZoF-g at mail dot gmail dot com>
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