tinytimer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/TODO +3 -0
- data/lib/tiny_timer.rb +6 -0
- data/lib/tiny_timer/dsl.rb +24 -0
- data/lib/tiny_timer/timer.rb +65 -0
- data/lib/tiny_timer/version.rb +3 -0
- data/test/examples.rb +44 -0
- data/tiny_timer.gemspec +23 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/TODO
ADDED
data/lib/tiny_timer.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module TinyTimer
|
2
|
+
# User Interface (global convenience methods)
|
3
|
+
module DSL
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def timer(desc=nil, &block)
|
8
|
+
TinyTimer::Timer.run_timer(desc, &block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def step(desc='', &block)
|
12
|
+
timer = TinyTimer::Timer.current_timer
|
13
|
+
# TODO: raise exeption if timer is nil
|
14
|
+
timer.step(desc,&block) if timer
|
15
|
+
end
|
16
|
+
|
17
|
+
def comment(desc='')
|
18
|
+
timer = TinyTimer::Timer.current_timer
|
19
|
+
# TODO: raise exeption if timer is nil
|
20
|
+
timer.comment(desc) if timer
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module TinyTimer
|
2
|
+
TIMER_CHARS = '*** '
|
3
|
+
LEADING_CHARS = TIMER_CHARS
|
4
|
+
STEP_CHARS = TIMER_CHARS + ' '
|
5
|
+
|
6
|
+
class Timer
|
7
|
+
@@timers = []
|
8
|
+
@@leading = ''
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def run_timer(desc=nil, &block)
|
12
|
+
# desc ||= Rake.application.last_description
|
13
|
+
new_timer = self.new
|
14
|
+
@@timers.push(new_timer)
|
15
|
+
@@leading += LEADING_CHARS if self.count > 1 # 4 whitespaces
|
16
|
+
puts "#{@@leading}#{TIMER_CHARS}Started: #{desc}" #TODO: ANSI color for Started
|
17
|
+
yield
|
18
|
+
puts "#{@@leading}#{TIMER_CHARS}Finished. Total running time: #{Time.now - new_timer.start_time} seconds"
|
19
|
+
@@leading.sub!(/.{#{LEADING_CHARS.size}}$/,'') if self.count > 1
|
20
|
+
@@timers.pop
|
21
|
+
end
|
22
|
+
|
23
|
+
def count
|
24
|
+
@@timers.size
|
25
|
+
end
|
26
|
+
|
27
|
+
def current_timer
|
28
|
+
@@timers.last
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :start_time, :real_running_time
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
@start_time = Time.now
|
36
|
+
@step_count = 0
|
37
|
+
@real_running_time = 0 # sum of the running time of all steps
|
38
|
+
end
|
39
|
+
|
40
|
+
# if no block given, should do nothing
|
41
|
+
def step(desc='')
|
42
|
+
if block_given?
|
43
|
+
if @block_flag
|
44
|
+
yield
|
45
|
+
else
|
46
|
+
@step_count += 1
|
47
|
+
puts "#{@@leading}#{STEP_CHARS}#{@step_count}. #{desc} ... "
|
48
|
+
@block_flag = true
|
49
|
+
start = Time.now
|
50
|
+
yield
|
51
|
+
running_time = Time.now - start
|
52
|
+
@block_flag = false
|
53
|
+
@real_running_time += running_time
|
54
|
+
puts "#{@@leading}#{STEP_CHARS}-- finished in #{running_time} seconds"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# use this method if you only want to print out something, do not create an empty step or timer
|
60
|
+
def comment(desc='')
|
61
|
+
puts "#{@@leading}#{STEP_CHARS}[ #{desc} ]" if desc.length > 0
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/test/examples.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# since I'm too naive to know how to test with the $stdout, this is a sample ruby script playing with tiny_timer :-p
|
2
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
3
|
+
require 'tiny_timer'
|
4
|
+
|
5
|
+
def nested_1
|
6
|
+
timer "(nested_1) yielding some block" do
|
7
|
+
step 'do something first' do
|
8
|
+
10000.times do |i|
|
9
|
+
i + 2
|
10
|
+
end
|
11
|
+
end
|
12
|
+
yield
|
13
|
+
step 'do something afterwards' do
|
14
|
+
step 'a nested step will be neglected, although the code in the block will still run' do # do not do this!
|
15
|
+
puts 'another screwing output'
|
16
|
+
end
|
17
|
+
10000.times do |i|
|
18
|
+
i + 2
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# a normal piece of code
|
25
|
+
timer 'how long does 10000 calculations take ?' do
|
26
|
+
step 'plusing' do
|
27
|
+
10000.times do |i|
|
28
|
+
i + 2
|
29
|
+
end
|
30
|
+
end
|
31
|
+
step 'multipying' do
|
32
|
+
10000.times do |i|
|
33
|
+
i*33
|
34
|
+
end
|
35
|
+
end
|
36
|
+
step 'dividing' do
|
37
|
+
10000.times do |i|
|
38
|
+
1000/(i+1)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
step 'if no block passed, should not crash and should print nothing'
|
42
|
+
nested_1 { puts "if any code prints something, it will screw things up"}
|
43
|
+
comment 'should be the end?'
|
44
|
+
end
|
data/tiny_timer.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "tiny_timer/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "tinytimer"
|
7
|
+
s.version = TinyTimer::VERSION
|
8
|
+
s.authors = ["Tao Zhang"]
|
9
|
+
s.email = ["birdbluebloc@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{ a very simple gem to print elapsed time for pieces of code or rake tasks }
|
12
|
+
s.description = %q{ with handy global methods, print out the running time as your codes run }
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
# s.rubyforge_project = "tiny_timer"
|
16
|
+
|
17
|
+
s.add_dependency('rake', ">= 0.8.7")
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tinytimer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tao Zhang
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-08-26 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 8
|
31
|
+
- 7
|
32
|
+
version: 0.8.7
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: " with handy global methods, print out the running time as your codes run "
|
36
|
+
email:
|
37
|
+
- birdbluebloc@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- Rakefile
|
48
|
+
- lib/TODO
|
49
|
+
- lib/tiny_timer.rb
|
50
|
+
- lib/tiny_timer/dsl.rb
|
51
|
+
- lib/tiny_timer/timer.rb
|
52
|
+
- lib/tiny_timer/version.rb
|
53
|
+
- test/examples.rb
|
54
|
+
- tiny_timer.gemspec
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: ""
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: a very simple gem to print elapsed time for pieces of code or rake tasks
|
87
|
+
test_files:
|
88
|
+
- test/examples.rb
|