quick_benchmark 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTZmYjg0ZjhhNGE5YWVlOGRhZmU5YTQwOGJkY2E0MzBlM2U2OWUyMg==
5
+ data.tar.gz: !binary |-
6
+ YTQwNDVhZDJkMWQ0ZTU3OGE2YWU2NzkwMGQzNjA4MDY0Zjk0Njk0Mw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Yjg5YjRhMWZlNjI0ZDBmNzZlYTJkMjgyMzMzZWYxZTQ2NmQwNDJkYWVlOTFh
10
+ YzBiYjc5OGZkYmVkM2IwNTBjMDY5ZWQxODU3NTBhOTRhNzU1YmJiMDZlZmVi
11
+ ZjJlZjVkYzAyZWNiYTk2N2IyODljNzZlMjM5OWM1ZWQ0MTQ3YjA=
12
+ data.tar.gz: !binary |-
13
+ M2ExMzlhOGEwMWMyMmRhZWQ0MzUyNGI5ZTNlN2IzNTRkY2Q5NjE5MTE2ODMy
14
+ N2M3YzJiZjhmMTZjZTI3OGM1YjU0NWQyNGFkY2UwYmY0OTllZDk5ZTdmZDMw
15
+ NDkzZWI2ZGQwNTY0ZjRiZWE3N2M5YzYyNjVkMWMzMzMyYTY0ZjM=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,38 @@
1
+ # quick_benchmark
2
+
3
+ Sometimes I'm curious which of a few choices is fastest, but I can never remember the benchmark syntax. Do not use this gem to benchmark anything non-trivial.
4
+
5
+ ```
6
+ $ gem install quick_benchmark
7
+ ```
8
+
9
+ ```ruby
10
+ require 'quick_benchmark'
11
+
12
+ nums = (0..99).to_a
13
+
14
+ benchmark 1000.times do
15
+
16
+ time :reject do
17
+ nums.reject(&:even?)
18
+ end
19
+
20
+ time 'select' do
21
+ nums.select(&:odd?)
22
+ end
23
+
24
+ end
25
+
26
+ # Rehearsal -------------------------------------------
27
+ # reject 0.000000 0.000000 0.000000 ( 0.000234)
28
+ # select 0.000000 0.000000 0.000000 ( 0.000225)
29
+ # ---------------------------------- total: 0.000000sec
30
+ #
31
+ # user system total real
32
+ # reject 0.000000 0.000000 0.000000 ( 0.000207)
33
+ # select 0.000000 0.000000 0.000000 ( 0.000207)
34
+ ```
35
+
36
+ ### On Optimization
37
+
38
+ As a word of warning, remember not to optimize prematurely. You're just wasting your time. 99/100 times you use `quick_benchmark` the results are going to be exactly the same (hence the example).
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/*test.rb']
7
+ t.verbose = true
8
+ end
@@ -0,0 +1,15 @@
1
+ require 'quick_benchmark'
2
+
3
+ nums = (0..99).to_a
4
+
5
+ benchmark 1000 do
6
+
7
+ time :reject do
8
+ nums.reject(&:even?)
9
+ end
10
+
11
+ time :select do
12
+ nums.select(&:odd?)
13
+ end
14
+
15
+ end
@@ -0,0 +1,19 @@
1
+ require 'quick_benchmark'
2
+
3
+ benchmark 1.million.times do
4
+
5
+ str = "a fairly long string but not too long"
6
+
7
+ time "split quotes" do
8
+ str.split('')
9
+ end
10
+
11
+ time "split regex" do
12
+ str.split(//)
13
+ end
14
+
15
+ time "chars to_a" do
16
+ str.chars.to_a
17
+ end
18
+
19
+ end
@@ -0,0 +1,37 @@
1
+ require 'quick_benchmark/version'
2
+ require 'quick_benchmark/fixnum'
3
+ require 'benchmark'
4
+
5
+ module QuickBenchmark
6
+
7
+ TimeTest = Struct.new :label, :method
8
+
9
+ def benchmark(iterations = 100, &tests)
10
+ @tests = []
11
+ tests.call
12
+ iterations = QuickBenchmark.iterations_given(iterations)
13
+ Benchmark.bmbm(7) do |x|
14
+ @tests.each do |test|
15
+ x.report(test.label) do
16
+ iterations.times { test.method }
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def self.iterations_given(amount)
23
+ if amount.respond_to? :count
24
+ amount.count
25
+ else
26
+ amount
27
+ end
28
+ end
29
+
30
+ def time(label, &method)
31
+ t = TimeTest.new label.to_s, method
32
+ @tests.push t
33
+ end
34
+
35
+ end
36
+
37
+ include QuickBenchmark
@@ -0,0 +1,14 @@
1
+ class Fixnum
2
+
3
+ def hundred
4
+ self * 100
5
+ end
6
+
7
+ [:thousand, :million, :billion].each_with_index do |name, i|
8
+ define_method name do
9
+ exp = i.next * 3
10
+ self * 10**exp
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,3 @@
1
+ module QuickBenchmark
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quick_benchmark/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'quick_benchmark'
8
+ spec.version = QuickBenchmark::VERSION
9
+ spec.authors = %w(aj0strow)
10
+ spec.email = %w(alexander.ostrow@gmail.com)
11
+ spec.description = 'Quick Benchmark'
12
+ spec.summary = 'simple DSL for quickly benchmarking small code blocks'
13
+ spec.homepage = 'http://github.com/aj0strow/quick_benchmark'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(lib)
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class QuickyBenchmarkTest < Test::Unit::TestCase
4
+
5
+ def test_fixnum
6
+ assert_equal 100, 1.hundred
7
+ assert_equal 1_000, 1.thousand
8
+ assert_equal 1_000_000, 1.million
9
+ assert_equal 1_000_000_000, 1.billion
10
+ end
11
+
12
+ def test_iterations_given
13
+ assert_equal 12, QuickBenchmark.iterations_given(12)
14
+ assert_equal 15, QuickBenchmark.iterations_given(15.times)
15
+ end
16
+
17
+ def test_time
18
+ do_end = QuickBenchmark.time :testing do
19
+ "With do end"
20
+ end
21
+ assert_equal "testing", do_end.first.label
22
+ assert_equal "With do end", do_end.last.method.call
23
+ end
24
+
25
+ def test_benchmark
26
+ val = benchmark 1.times do
27
+ time(:cool) { puts 'hey' }
28
+ end
29
+ assert_equal Benchmark::Tms, val.first.class
30
+ end
31
+
32
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ require 'quick_benchmark'
3
+
4
+ module QuickBenchmark
5
+ @tests = []
6
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quick_benchmark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - aj0strow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Quick Benchmark
42
+ email:
43
+ - alexander.ostrow@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - examples/select_reject.rb
53
+ - examples/split.rb
54
+ - lib/quick_benchmark.rb
55
+ - lib/quick_benchmark/fixnum.rb
56
+ - lib/quick_benchmark/version.rb
57
+ - quick_benchmark.gemspec
58
+ - test/quick_benchmark_test.rb
59
+ - test/test_helper.rb
60
+ homepage: http://github.com/aj0strow/quick_benchmark
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.5
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: simple DSL for quickly benchmarking small code blocks
84
+ test_files:
85
+ - test/quick_benchmark_test.rb
86
+ - test/test_helper.rb