asymptotic 0.0.2 → 0.0.3
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.
- data/README.md +4 -4
- data/lib/asymptotic/graph.rb +14 -8
- data/lib/asymptotic/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# asymptotic.gem
|
2
2
|
|
3
3
|
Create runtime-analysis graphs with minimal effort.
|
4
4
|
|
@@ -24,13 +24,13 @@ require 'asymptotic'
|
|
24
24
|
Asymptotic::Graph::plot("Data-Structure Element Retrieval Methods",
|
25
25
|
|
26
26
|
check_if_element_in_array: {
|
27
|
-
function: ->(shuffled_array)
|
27
|
+
function: ->(shuffled_array){ shuffled_array.include? 1 },
|
28
28
|
input_seeds: (1..1000),
|
29
29
|
input_function: ->(limit){ (1..limit*1000).to_a.shuffle }
|
30
30
|
},
|
31
31
|
|
32
32
|
check_if_key_in_hash: {
|
33
|
-
function: ->(hash)
|
33
|
+
function: ->(hash){ hash.has_key? 'my key' },
|
34
34
|
input_seeds: (1..1000),
|
35
35
|
input_function: ->(limit){
|
36
36
|
hash = {}.tap { |h|
|
@@ -43,7 +43,7 @@ Asymptotic::Graph::plot("Data-Structure Element Retrieval Methods",
|
|
43
43
|
)
|
44
44
|
|
45
45
|
```
|
46
|
-

|
47
47
|
|
48
48
|
## Contributing
|
49
49
|
|
data/lib/asymptotic/graph.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Asymptotic
|
2
2
|
class Graph
|
3
3
|
|
4
|
-
def initialize(problem, algorithm_hash)
|
4
|
+
def initialize(attempts = 5, problem, algorithm_hash)
|
5
|
+
@attempts = attempts
|
5
6
|
@problem = problem
|
6
7
|
@algorithm_hash = algorithm_hash
|
7
8
|
end
|
@@ -11,7 +12,7 @@ module Asymptotic
|
|
11
12
|
Gnuplot::Plot.new(gnuplot) do |plot|
|
12
13
|
plot.title "Asymptotic Analysis of #{@problem} (#{`ruby -v`.split(' (').first})"
|
13
14
|
plot.xlabel "Input size"
|
14
|
-
plot.ylabel "Time taken"
|
15
|
+
plot.ylabel "Time taken in seconds"
|
15
16
|
|
16
17
|
|
17
18
|
@algorithm_hash.each do |name, function_hash|
|
@@ -21,13 +22,18 @@ module Asymptotic
|
|
21
22
|
function = function_hash[:function]
|
22
23
|
sizes = []
|
23
24
|
runtimes = seeds.map do |seed|
|
24
|
-
|
25
|
-
|
25
|
+
size = 0
|
26
|
+
times_taken = ([0] * @attempts).map do
|
27
|
+
input = input_generation_function.(seed)
|
28
|
+
size = input.size
|
29
|
+
GC.disable
|
30
|
+
time_taken = Benchmark.realtime { function[input] }
|
31
|
+
GC.enable
|
32
|
+
time_taken
|
33
|
+
end
|
34
|
+
sizes << size
|
26
35
|
print '.'.yellow
|
27
|
-
|
28
|
-
time_taken = Benchmark.realtime { function[input] }
|
29
|
-
GC.enable
|
30
|
-
time_taken
|
36
|
+
average_time_taken = times_taken.inject(:+) / @attempts.to_f
|
31
37
|
end
|
32
38
|
|
33
39
|
points = [sizes, runtimes]
|
data/lib/asymptotic/version.rb
CHANGED