-
-
Save Papierkorb/5ac54d2764d234e69bea244ae2d35111 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env crystal run | |
buf = Bytes.new 4096 | |
ints = Slice(UInt64).new(buf.pointer(4096).as(UInt64*), buf.size / sizeof(UInt64)) | |
words = 1 | |
lines = 0 | |
bytes_total = 0 | |
while (bytes = ARGF.read buf) > 0 | |
bytes_total += bytes | |
if bytes != buf.size # Slow path? | |
buf.each do |byte| | |
words += 1 if byte == 0x20 | |
end | |
else | |
ints.each do |qword| | |
v = qword ^ (UInt64::MAX / 0xFFu64 * 0x20u64) | |
if ((v - 0x0101010101010101u64) & ~v & 0x8080808080808080u64) > 0 | |
words += (v & 0xFF00000000000000u64 == 0).hash | |
words += (v & 0x00FF000000000000u64 == 0).hash | |
words += (v & 0x0000FF0000000000u64 == 0).hash | |
words += (v & 0x000000FF00000000u64 == 0).hash | |
words += (v & 0x00000000FF000000u64 == 0).hash | |
words += (v & 0x0000000000FF0000u64 == 0).hash | |
words += (v & 0x000000000000FF00u64 == 0).hash | |
words += (v & 0x00000000000000FFu64 == 0).hash | |
end | |
end | |
end | |
end | |
pp words, lines, bytes_total |
Author
Papierkorb
commented
Nov 15, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment