Last active
December 22, 2015 22:59
-
-
Save litch/6543765 to your computer and use it in GitHub Desktop.
Storing Regex in a constant instead of a method saves a lot of memory, but not a lot of time.
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
?> def get_memory_usage | |
>> `ps -o rss= -p #{Process.pid}`.to_i | |
>> end | |
=> nil | |
>> | |
?> before = get_memory_usage | |
=> 20296 | |
>> | |
?> n.times {MethodRegexObject.new} | |
=> 5000000 | |
>> | |
?> after = get_memory_usage | |
=> 23484 | |
>> print "Method Regex Usage: " + (after-before).to_s | |
Method Regex Usage: 3188=> nil | |
>> | |
?> before = get_memory_usage | |
=> 23484 | |
>> | |
?> n.times {ConstantRegexObject.new} | |
=> 5000000 | |
>> | |
?> after = get_memory_usage | |
=> 24208 | |
>> print "Constant Regex Usage: " + (after-before).to_s | |
Constant Regex Usage: 724=> nil | |
?> | |
?> Benchmark.bmbm do |x| | |
?> x.report("Method") { n.times {MethodRegexObject.new} } | |
>> x.report("Constant") { n.times {ConstantRegexObject.new} } | |
>> | |
?> end | |
Rehearsal -------------------------------------------- | |
Method 17.080000 0.070000 17.150000 ( 17.155665) | |
Constant 16.750000 0.120000 16.870000 ( 16.867394) | |
---------------------------------- total: 34.020000sec | |
user system total real | |
Method 17.120000 0.350000 17.470000 ( 17.470644) | |
Constant 16.700000 0.300000 17.000000 ( 17.007322) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment