5 ms·
The dates are strictly formatted, the values are stored as UTF8 and there are no cultures to consider. I wonder what the speedup will be from omitting all the
by userbinator 10y ago
The dates are strictly formatted, the values are stored as UTF8 and there are no cultures to consider.
I wonder what the speedup will be from omitting all the validation and associated branching and just picking the values out directly. Chances are that you won't see more than one date format in the same input either. Another strategy I'd use is to parallelise the digitisation; assuming the YYYY-MM-DDTHH:mm:ss format, it'd start off something like this (untested, but should be close if not completely correct):
Y = *(uint32_t*)s - 0x30303030;
M = *(uint16_t*)(s+5) - 0x3030;
D = *(uint16_t*)(s+8) - 0x3030;
Y = 100 * ((10 * (Y >> 24) + (Y >> 16)) & 255) +
((10 * (Y >> 8) + Y) & 255);
M = (10 * (M >> 8) + M) & 255;
D = (10 * (D >> 8) + D) & 255;
It should be possible to reduce the year computation to 2 multiplications, at the expense of several more masking operations. Around 50-100 clock cycles per full datetime should be possible, which is in the few-dozen-ns range on a GHz-clock CPU. Probably could go a bit faster still if you start bringing in the SSE...
- avian 10y agoThis is quite clever. The catch is that it assumes implementation specific details (like a little-endian architecture) while OP's code is (probably) portable C.
- dimman 10y agoWhich part are you referring to? Bit operations etc isn't affected by endianess. It looks like it would only potentially be a problem if input is coming from another system (haven't looked at where input comes from or what format/type).
- avian 10y agoI was referring to this part: Y = *(uint32_t*)s - 0x30303030; By casting a string to a uint32_t, you are assuming the memory layout of uint32_t. Y will have a different value depending on architecture. E.g. on a typical little-endian you will get 0x06010002. On big-endian you will get 0x02000106. I'm talking about C, however. I didn't notice the post is talking about C#. Things might be better defined there.
- JoeAltmaier 10y agoRegardless of layout, that subtraction will have the same effect on most printable characters. An identical effect on digits.
- avian 10y agoThe cast is the problematic part, not the subtraction.
- JoeAltmaier 10y agoIf there is no carry, then I don't understand. Four bytes will have the same value deducted. regardless of the byte order. If there is no carry implication (all are digits) then there is no issue.
- huhtenberg 10y agoOn RISC platforms *(uint32_t*)s will get you a SIGBUS if s is not aligned to a 4-byte boundary. Though that's another issue, not what OP was referring to.
- zeusk 10y agoARM has unaligned access since v6 (introduced in 2001); if you're on linux, unaligned access will be patched by the kernel (as was the case prior ARMv6 and even for MIPS afaik). Anyway, the point of his post was about possible gains from removing validation, not about being portable or production code.
- larvyde 10y agoThe subtraction isn't the issue, the cast is. the string "2016" is represented by the byte sequence [0x32, 0x30, 0x31, 0x36]. Casting this array to a uint32* in big endian gives you the integer 0x32303136 (or 842019126) while in little endian gives you the integer 0x36313032 (or 909193266).
- to3m 10y agoY>>24 will be the byte at +3 on a little-endian system, or +0 on a big-endian system. It's therefore the digit in the 10^0 column in the former case, or the 10^3 column in the latter. So it actually looks like it currently assumes a big-endian system.
- yread 10y agoOP's code is unsafe C#
- yread 10y agoSince you're mostly interested in only this century you could probably use lookup tables for everything.