Last active
August 21, 2024 00:43
-
-
Save apankrat/20776d68d1d97bca12576a6e204b2f74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Given a log line that starts like this: | |
* | |
* 2014.01.27 19:35:51.639 (UTC-1) 3 1 ... | |
* | |
* the following takes about ~60 ms per call in a release build | |
* produced by a VC++ 2017 using its stock CRT (v141): | |
*/ | |
sscanf(line, | |
"%hu.%hu.%hu %hu:%hu:%hu.%hu (UTC%d) %c %u %n", | |
&t.wYear, &t.wMonth, &t.wDay, | |
&t.wHour, &t.wMinute, &t.wSecond, &t.wMilliseconds, | |
&foo, &type_sym, &level, &read); | |
/* | |
* if the same is done by simply advancing a ptr through the line | |
* and doing the cs101-style number parsing, it runs in... 0.2 ms | |
* | |
* that's 300 times faster | |
*/ | |
src.ptr = line; | |
src.end = line + strlen(line); | |
if (src.size() < 36) | |
return false; | |
if (! parse_number(src, t.wYear) || | |
! consume_char(src, '.') || | |
! parse_number(src, t.wMonth) || | |
! consume_char(src, '.') || | |
! parse_number(src, t.wDay) || | |
! consume_char(src, ' ') || | |
! parse_number(src, t.wHour) || | |
! consume_char(src, ':') || | |
! parse_number(src, t.wMinute) || | |
! consume_char(src, ':') || | |
! parse_number(src, t.wSecond) || | |
! consume_char(src, '.') || | |
! parse_number(src, t.wMilliseconds) || | |
! consume_str (src, " (UTC") || | |
! consume_num (src) || | |
! consume_str (src, ") ") || | |
! parse_char (src, type_sym) || | |
! consume_char(src, ' ') || | |
! parse_number(src, level) || | |
! consume_char(src, ' ')) | |
{ | |
return false; | |
} | |
/* | |
* tl;dr - don't use stock MSVC sscanf() in fast paths | |
*/ |
Author
apankrat
commented
Oct 1, 2019
•
parse_number()
obviously doesn't check for the overflows, but that's trivial to add and with very little penalty.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment