Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cema-sp committed Jul 19, 2016
1 parent ee5e138 commit d3bc428
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
# Ignore Vim swap files
*.swp

# Ignore data files
*.csv

16 changes: 16 additions & 0 deletions 003_wrapper_ex.rb
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

12 changes: 12 additions & 0 deletions 004_array_bang.rb
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

12 changes: 12 additions & 0 deletions 004_string_bang.rb
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

36 changes: 36 additions & 0 deletions 005_files.rb
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

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ See [001_gc.rb].

See [002_memory.rb]

GC::Profiler has memory and CPU overhead (See [wrapper.rb] for custom wrapper example).

Save memory by avoiding copiyng objects, modify them in place if it is possible (use ! methods).

See [004_string_bang.rb]

If your String is less than 40 bytes - user '<<', not '+=' method to concatenate it and Ruby will not allocate additional object.

See [004_array_bang.rb] (w/ GC)

Read files line by line. And keep in mind not only total memory consumption but also peaks.

See [005_files.rb] (w/ and w/o GC)

39 changes: 39 additions & 0 deletions wrapper.rb
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

0 comments on commit d3bc428

Please sign in to comment.