Skip to content

Commit 9eb2a0f

Browse files
committedDec 9, 2012
v0.0.5 Major (15-20x) performance improvements
Looking at performance for the first time since hacking out these scripts, found major improvements in reach effort.
1 parent b8d68b8 commit 9eb2a0f

File tree

8 files changed

+57
-62
lines changed

8 files changed

+57
-62
lines changed
 

‎CHANGES

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changes
2+
3+
## v0.0.5
4+
5+
* Ridiculous (15-20x) speed improvements.
6+
* Fixed "--bundled" option
7+
8+
## v0.0.2 - 0.0.4
9+
10+
* Gemified
11+
* minor changes and improvements
12+
13+
## v0.0.1
14+
15+
* Just a hacked-out bundle of scripts.

‎Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
keyboard_battle (0.0.1)
4+
keyboard_battle (0.0.5)
55

66
GEM
77
remote: https://rubygems.org/

‎keyboard_battle-0.0.5.gem

147 KB
Binary file not shown.

‎lib/keyboard_battle/command_dispatcher.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ module KeyboardBattle
22
class CommandDispatcher
33

44
def initialize(args)
5+
56
if opt = args.first
67
case opt
78
when '--bundled'
8-
self.run(Dir.glob('texts/*.txt'))
9+
self.run(Dir.glob("#{File.expand_path('../../..', __FILE__)}/texts/*.txt"))
910
else
1011
self.run(args)
1112
end

‎lib/keyboard_battle/exercise.rb

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
module KeyboardBattle
22
class Exercise
33

4-
attr_accessor :results, :filename
4+
attr_accessor :results, :filename, :keyboards
55

6-
def initialize(filename, keyboards)
7-
@filename = filename
8-
@keyboards = keyboards
6+
def initialize(f, k)
7+
@filename = f
8+
@keyboards = k
99
@results = run
1010
end
1111

1212
private
1313

14-
attr_accessor :keyboards
15-
1614
# tests a passage of text for reach effort expended (zero for home row,
1715
# increasing for reach), and hand alternation effort. In both values,
1816
# lower is better.
@@ -26,23 +24,24 @@ def run
2624
open_and_process(filename,'r') do |file|
2725
while line = file.gets
2826
line.each_char do |char|
29-
if keyboard.combined.include?(char)
27+
if effort = keyboard::MAP[char]
3028

3129
# measure alternation efficiency
32-
hand = keyboard.left.include?(char) ? 'l' : 'r'
30+
hand = keyboard::LEFT_KEYS.include?(char) ? 'l' : 'r'
3331
if prev_hand
3432
alternation_effort += (hand == prev_hand) ? 1 : 0
3533
end
3634
prev_hand = hand
3735

3836
# measure reach efficiency
39-
reach_effort += EFFORT[keyboard.combined.find_index(char)]
37+
reach_effort += effort
4038
end
39+
4140
end
4241
end
4342
end
4443

45-
results[keyboard.name.to_sym] = {
44+
results[keyboard::NAME.to_sym] = {
4645
:alternation_effort => alternation_effort,
4746
:reach_effort => reach_effort,
4847
:raw_score => (alternation_effort + reach_effort)
@@ -57,10 +56,5 @@ def open_and_process(*args)
5756
f.close()
5857
end
5958

60-
EFFORT = ( # left hand + right hand effort values
61-
%w(3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2 3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2) +
62-
%w(2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1)
63-
).collect { |el| el.to_i }
64-
6559
end
6660
end

‎lib/keyboard_battle/keyboard.rb

+28-43
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,45 @@
11
module KeyboardBattle
22
class Keyboard
33

4-
class Qwerty < self
5-
def name
6-
"qwerty"
7-
end
4+
class << self
85

9-
def left
10-
%w(` 1 2 3 4 5 6 q w e r t a s d f g z x c v b ~ ! @ # $ % ^ Q W E R T A S D F G Z X C V B)
6+
def all
7+
[
8+
KeyboardBattle::Keyboard::Qwerty,
9+
KeyboardBattle::Keyboard::Dvorak,
10+
KeyboardBattle::Keyboard::Colemak
11+
]
1112
end
1213

13-
def right
14-
%w(7 8 9 0 - = y u i o p [ ] \\ h j k l ; ' n m , . / & * ( ) _ + Y U I O P { } | H J K L : " N M < > ?)
15-
end
1614
end
1715

18-
class Dvorak < self
19-
def name
20-
"dvorak"
21-
end
16+
LEFT_EFFORT = %w(3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2 3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2).collect { |el| el.to_i }
17+
RIGHT_EFFORT = %w(2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1).collect { |el| el.to_i }
18+
COMBINED_EFFORT = LEFT_EFFORT + RIGHT_EFFORT
2219

23-
def left
24-
%w(` 1 2 3 4 5 6 ' , . p y a o e u i ; q j k x ~ ! @ # $ % ^ " < > P Y A O E U I : Q J K X)
25-
end
26-
27-
def right
28-
%w(7 8 9 0 [ ] f g c r l / = \\ d h t n s - b m w v z & * ( ) { } F G C R L ? + | D H T N S _ B M W V Z)
29-
end
20+
class Qwerty < Keyboard
21+
NAME = "qwerty"
22+
LEFT_KEYS = %w(` 1 2 3 4 5 6 q w e r t a s d f g z x c v b ~ ! @ # $ % ^ Q W E R T A S D F G Z X C V B)
23+
RIGHT_KEYS = %w(7 8 9 0 - = y u i o p [ ] \\ h j k l ; ' n m , . / & * ( ) _ + Y U I O P { } | H J K L : " N M < > ?)
24+
COMBINED_KEYS = LEFT_KEYS + RIGHT_KEYS
25+
MAP = Hash[COMBINED_KEYS.zip(Keyboard::COMBINED_EFFORT)]
3026
end
3127

32-
class Colemak < self
33-
def name
34-
"colemak"
35-
end
36-
37-
def left
38-
%w(` 1 2 3 4 5 6 q w f p g a r s t d z x c v b ~ ! @ # $ % ^ Q W F P G A R S T D Z X C V B)
39-
end
40-
41-
def right
42-
%w(7 8 9 0 - = j l u y : [ ] \\ h n e i o ' k m , . / & * ( ) _ + J L U Y ; { } | H N E I O " K M < > ?)
43-
end
28+
class Dvorak < Keyboard
29+
NAME = "dvorak"
30+
LEFT_KEYS = %w(` 1 2 3 4 5 6 ' , . p y a o e u i ; q j k x ~ ! @ # $ % ^ " < > P Y A O E U I : Q J K X)
31+
RIGHT_KEYS = %w(7 8 9 0 [ ] f g c r l / = \\ d h t n s - b m w v z & * ( ) { } F G C R L ? + | D H T N S _ B M W V Z)
32+
COMBINED_KEYS = LEFT_KEYS + RIGHT_KEYS
33+
MAP = Hash[COMBINED_KEYS.zip(Keyboard::COMBINED_EFFORT)]
4434
end
4535

46-
attr_accessor :left, :right, :name
47-
48-
def combined
49-
left + right
36+
class Colemak < Keyboard
37+
NAME = "colemak"
38+
LEFT_KEYS = %w(` 1 2 3 4 5 6 q w f p g a r s t d z x c v b ~ ! @ # $ % ^ Q W F P G A R S T D Z X C V B)
39+
RIGHT_KEYS = %w(7 8 9 0 - = j l u y : [ ] \\ h n e i o ' k m , . / & * ( ) _ + J L U Y ; { } | H N E I O " K M < > ?)
40+
COMBINED_KEYS = LEFT_KEYS + RIGHT_KEYS
41+
MAP = Hash[COMBINED_KEYS.zip(Keyboard::COMBINED_EFFORT)]
5042
end
5143

52-
def self.all
53-
[
54-
KeyboardBattle::Keyboard::Qwerty.new,
55-
KeyboardBattle::Keyboard::Dvorak.new,
56-
KeyboardBattle::Keyboard::Colemak.new
57-
]
58-
end
5944
end
6045
end

‎lib/keyboard_battle/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module KeyboardBattle
2-
VERSION = "0.0.4"
2+
VERSION = "0.0.5"
33
end

‎spec/keyboard_battle/exercise_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let(:exercise) { KeyboardBattle::Exercise.new('texts/qbf.txt', KeyboardBattle::Keyboard.all) }
77

88
it "loads the keyboards" do
9-
expect(exercise.send(:keyboards).map {|k| k.name}).to eq(%w(qwerty dvorak colemak))
9+
expect(exercise.send(:keyboards).map {|k| k::NAME}).to eq(%w(qwerty dvorak colemak))
1010
end
1111

1212
describe "when processing texts" do

0 commit comments

Comments
 (0)
Please sign in to comment.