Skip to content

Instantly share code, notes, and snippets.

@vzctl
Created December 17, 2015 12:28
Show Gist options
  • Save vzctl/3f86e72feab5bca98b06 to your computer and use it in GitHub Desktop.
Save vzctl/3f86e72feab5bca98b06 to your computer and use it in GitHub Desktop.
require 'benchmark'
require './puppet/lib/puppet/util/puppetdb/char_encoding'
include Puppet::Util::Puppetdb::CharEncoding
random_bytes = File.read('/dev/urandom', 10)
strings = 5.times.map { |i| ("0" * 1024 * (i+1)*100 + random_bytes).force_encoding('UTF-8') }
Benchmark.bm(1) do |x|
x.report('str.index') { strings.last.index(DEFAULT_INVALID_CHAR) }
strings.each do |str|
x.report("all_indexes_of_char (#{str.size} bytes)") do
Puppet::Util::Puppetdb::CharEncoding.all_indexes_of_char(str, DEFAULT_INVALID_CHAR)
end
end
end
user system total real
str.index 0.000000 0.000000 0.000000 ( 0.000060)
all_indexes_of_char (102408 bytes) 0.250000 0.000000 0.250000 ( 0.259929)
all_indexes_of_char (204808 bytes) 0.970000 0.010000 0.980000 ( 0.977001)
all_indexes_of_char (307208 bytes) 2.240000 0.010000 2.250000 ( 2.264098)
all_indexes_of_char (409608 bytes) 4.180000 0.010000 4.190000 ( 4.206656)
all_indexes_of_char (512008 bytes) 6.740000 0.010000 6.750000 ( 6.779366)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment