Created
March 8, 2021 02:38
-
-
Save corroded/f68303b802258f28dd49b48fc1533a56 to your computer and use it in GitHub Desktop.
Quick benchmark of delete_prefix, sub, and gsub based off https://twitter.com/JemmaIssroff/status/1368204231476469760?s=20
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
require 'benchmark/ips' | |
Benchmark.ips do |bm| | |
MAX_LENGTH = 280 | |
long_string = 'YOLO: the return of the comeback' | |
bm.report('delete_prefix') do | |
long_string.delete_prefix('YOLO: ') | |
end | |
bm.report('delete_prefix!') do | |
long_string.delete_prefix!('YOLO: ') | |
end | |
bm.report('sub') do | |
long_string.sub('YOLO: ', '') | |
end | |
bm.report('sub!') do | |
long_string.sub!('YOLO: ', '') | |
end | |
bm.report('gsub') do | |
long_string.gsub('YOLO: ', '') | |
end | |
bm.report('gsub!') do | |
long_string.gsub!('YOLO: ', '') | |
end | |
bm.compare! | |
end |
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
Warming up -------------------------------------- | |
delete_prefix 503.777k i/100ms | |
delete_prefix! 938.312k i/100ms | |
sub 364.851k i/100ms | |
sub! 446.770k i/100ms | |
gsub 408.128k i/100ms | |
gsub! 460.125k i/100ms | |
Calculating ------------------------------------- | |
delete_prefix 7.569M (± 3.4%) i/s - 38.287M in 5.064362s | |
delete_prefix! 9.272M (± 2.1%) i/s - 46.916M in 5.062187s | |
sub 3.767M (± 4.6%) i/s - 18.972M in 5.048189s | |
sub! 4.716M (± 1.6%) i/s - 23.679M in 5.021810s | |
gsub 4.124M (± 2.5%) i/s - 20.815M in 5.050811s | |
gsub! 4.708M (± 4.9%) i/s - 23.926M in 5.097267s | |
Comparison: | |
delete_prefix!: 9272140.6 i/s | |
delete_prefix: 7569230.2 i/s - 1.22x (± 0.00) slower | |
sub!: 4716448.4 i/s - 1.97x (± 0.00) slower | |
gsub!: 4708396.1 i/s - 1.97x (± 0.00) slower | |
gsub: 4123799.2 i/s - 2.25x (± 0.00) slower | |
sub: 3766876.1 i/s - 2.46x (± 0.00) slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment