Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Created November 15, 2016 23:55
Show Gist options
  • Save Papierkorb/5ac54d2764d234e69bea244ae2d35111 to your computer and use it in GitHub Desktop.
Save Papierkorb/5ac54d2764d234e69bea244ae2d35111 to your computer and use it in GitHub Desktop.
#!/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
@Papierkorb
Copy link
Author

words       # => 17712435
lines       # => 0
bytes_total # => 104857408
./wc foo  0,04s user 0,02s system 95% cpu 0,059 total

        0  17712400 104857408 foo
wc foo  0,88s user 0,01s system 99% cpu 0,901 total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment