I've got this string: "37:48.1234567"
(there's a colon and there's a decimal in there)
I need to remove everything from the decimal and past it.
The number before the decimal could be any length (ie. 1:23:39.12357
).
str.split(".")[0]
Hope that helps!
I made a little benchmark with the solutions up to now. I modified the solutions a bit.
One unclear problem: Whats the result for 1:23:39.123.57
? 1:23:39
or 1:23:39.123
?
I choosed 1:23:39
, only test regex has the other solution (see Tims answer)
The split
variant has 2x2 variants: [0]
or .first
and split with a 2nd parameter , 2
. The 2nd parameter of split limit the number of the splitted elements. We need only the first, so we may split in first and rest.
The fastes solution: split('.', 2)
. If you use first
or [0]
makes no difference. I would prefer first.
The result ( ruby 1.9.2p290
):
Rehearsal -------------------------------------------------
regex 0.141000 0.000000 0.141000 ( 0.140625)
regex2 0.125000 0.000000 0.125000 ( 0.125000)
split[0] 0.031000 0.000000 0.031000 ( 0.031250)
split[0],2 0.047000 0.000000 0.047000 ( 0.046875)
split.first 0.031000 0.000000 0.031000 ( 0.031250)
split.first,2 0.031000 0.000000 0.031000 ( 0.031250)
---------------------------------------- total: 0.406000sec
user system total real
regex 0.125000 0.000000 0.125000 ( 0.125000)
regex2 0.109000 0.015000 0.124000 ( 0.125000)
split[0] 0.031000 0.000000 0.031000 ( 0.031250)
split[0],2 0.032000 0.000000 0.032000 ( 0.031250)
split.first 0.047000 0.000000 0.047000 ( 0.046875)
split.first,2 0.031000 0.000000 0.031000 ( 0.031250)
The result ( ruby 1.8.7p352
):
Rehearsal -------------------------------------------------
regex 0.060000 0.000000 0.060000 ( 0.060661)
regex2 0.050000 0.000000 0.050000 ( 0.049141)
split[0] 0.080000 0.000000 0.080000 ( 0.083703)
split[0],2 0.070000 0.000000 0.070000 ( 0.072780)
split.first 0.080000 0.000000 0.080000 ( 0.080419)
split.first,2 0.070000 0.000000 0.070000 ( 0.068370)
---------------------------------------- total: 0.410000sec
user system total real
regex 0.060000 0.000000 0.060000 ( 0.055427)
regex2 0.050000 0.000000 0.050000 ( 0.048538)
split[0] 0.080000 0.000000 0.080000 ( 0.084225)
split[0],2 0.070000 0.000000 0.070000 ( 0.071673)
split.first 0.070000 0.000000 0.070000 ( 0.079865)
split.first,2 0.060000 0.000000 0.060000 ( 0.068685)
The Benchmark:
require 'benchmark'
TESTDATA = %w{
37:48.1234567
1:23:39.12357
1:23:39.123.57
}
N = 10_000 #Number of Test loops
Benchmark.bmbm(10) {|b|
b.report('regex') { N.times { TESTDATA.each{|str| str.gsub(/\.[^.]*\Z/, '') }} }
b.report('regex2') { N.times { TESTDATA.each{|str| str.gsub(/\..*\Z/, '') }} }
b.report('split[0]') { N.times { TESTDATA.each{| str | str.split(".")[0] }} }
b.report('split[0],2') { N.times { TESTDATA.each{| str | str.split(".",2)[0] }} }
b.report('split.first') { N.times { TESTDATA.each{| str | str.split(".").first }} }
b.report('split.first,2') { N.times { TESTDATA.each{| str | str.split(".",2).first }} }
#~ b.report('') { N.times { TESTDATA.each{| str | }} }
#~ b.report('') { N.times { TESTDATA.each{|t| }} }
#~ b.report('') { N.times { TESTDATA.each{|t| }} }
} #Benchmark
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With