-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ | |
# Ignore Vim swap files | ||
*.swp | ||
|
||
# Ignore data files | ||
*.csv | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'wrapper' | ||
require 'csv' | ||
|
||
measure do | ||
data = CSV.open('003_10mb.csv') | ||
output = data.readlines.map do |line| | ||
line.map do |col| | ||
col.downcase.gsub(/\b('?[a-z])/) { $1.capitalize } | ||
end | ||
end | ||
|
||
File.open('003_10mb_output.csv', 'w+') do |file| | ||
file.write output.join('\n') | ||
end | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'wrapper' | ||
|
||
data = Array.new(100) { 'x' * 1024 * 1024 } # 100 Mb | ||
|
||
measure do | ||
data.map { |str| str.upcase } | ||
end | ||
|
||
measure do | ||
data.map! { |str| str.upcase! } | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'wrapper' | ||
|
||
str = 'X' * 1024 * 1024 * 10 # 10Mb string | ||
|
||
measure do | ||
str = str.downcase | ||
end | ||
|
||
measure do | ||
str = str.downcase! | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'wrapper' | ||
require 'csv' | ||
|
||
puts 'Just read file in memory:' | ||
measure do | ||
File.read('003_10mb.csv') | ||
end | ||
|
||
puts 'Read file and manipulate string:' | ||
measure do | ||
File.readlines('003_10mb.csv').map! do |line| | ||
line.split(',') | ||
end | ||
end | ||
|
||
puts 'Just read file in memory with CSV lib:' | ||
measure do | ||
CSV.read('003_10mb.csv') | ||
end | ||
|
||
puts 'Read file line by line:' | ||
measure do | ||
file = File.open('003_10mb.csv', 'r') | ||
while line = file.gets | ||
line.split(',') | ||
end | ||
end | ||
|
||
puts 'Read file line by line with CSV lib:' | ||
measure do | ||
file = CSV.open('003_10mb.csv') | ||
while line = file.readline | ||
line | ||
end | ||
end | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'json' | ||
require 'benchmark' | ||
require 'ap' | ||
|
||
def measure(&block) | ||
no_gc = (ARGV[0] == '--no-gc') | ||
|
||
if no_gc | ||
GC.disable | ||
else | ||
GC.start | ||
end | ||
|
||
memory_before = `ps -o rss= -p #{Process.pid}`.to_i/1024 | ||
gc_stats_before = GC.stat | ||
|
||
time = Benchmark.realtime do | ||
yield | ||
end | ||
|
||
puts "Objects: #{ObjectSpace.count_objects}" | ||
unless no_gc | ||
GC.start(full_mark: true, immediate_sweep: true, immediate_mark: false) | ||
puts "Objects after GC: #{ObjectSpace.count_objects}" | ||
end | ||
|
||
gc_stats_after = GC.stat | ||
memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024 | ||
|
||
ap({ | ||
RUBY_VERSION => { | ||
gc: no_gc ? 'disabled' : 'enabled', | ||
time: time.round(2), | ||
gc_count: gc_stats_after[:count] - gc_stats_before[:count], | ||
memory: "%d MB" % (memory_after - memory_before) | ||
} | ||
}) | ||
end | ||
|