darvin_functions 0.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/darvin_functions.c +19 -17
- data/test/benchmark_mmp.rb +22 -0
- data/test/test_mmp.rb +16 -33
- metadata +33 -59
- data/README.rdoc +0 -7
- data/test/helper.rb +0 -10
data/ext/darvin_functions.c
CHANGED
@@ -6,9 +6,6 @@
|
|
6
6
|
|
7
7
|
VALUE mDarvinFunctions;
|
8
8
|
|
9
|
-
/********************************************************************************
|
10
|
-
* roundNumber
|
11
|
-
********************************************************************************/
|
12
9
|
long roundNumber(double value) {
|
13
10
|
if (value == 0.0) return (0); // don't mess with zero
|
14
11
|
if (value > 0.0) return ((long)(value+.5)); // positive number
|
@@ -17,21 +14,23 @@ long roundNumber(double value) {
|
|
17
14
|
|
18
15
|
/* vself is a reference to the module or itself
|
19
16
|
varray is a Ruby array */
|
20
|
-
VALUE
|
17
|
+
VALUE mDarvinFunctions_nkmax(VALUE vself, VALUE varray) {
|
18
|
+
VALUE new_array;
|
19
|
+
int i, j, trail, offset;
|
20
|
+
long *tot; // total for specific interval
|
21
|
+
long *maxTot; // current sum for (i-1)
|
22
|
+
|
23
|
+
(void)vself; // silence unused variable warning message
|
24
|
+
|
21
25
|
if (NIL_P(varray) || RARRAY_LEN(varray) == 0)
|
22
26
|
return varray;
|
23
27
|
|
24
|
-
|
25
|
-
|
26
|
-
int i, j, trail, offset;
|
27
|
-
long *tot; // total for specific interval
|
28
|
-
long *maxTot; // current sum for (i-1)
|
28
|
+
new_array = rb_ary_new2(RARRAY_LEN(varray));
|
29
29
|
|
30
30
|
tot = ALLOCA_N(long, RARRAY_LEN(varray)); // allocate memory
|
31
31
|
maxTot = ALLOCA_N(long, RARRAY_LEN(varray)); // allocate memory
|
32
32
|
|
33
33
|
trail = 0; // trailing value
|
34
|
-
memset(maxTot, '\0', sizeof maxTot); // init max totals by slot
|
35
34
|
|
36
35
|
maxTot[0] = tot[0] = FIX2INT(RARRAY_PTR(varray)[0]); // need first total (for single cell average), seed for others
|
37
36
|
|
@@ -45,25 +44,28 @@ VALUE mDarvinFunctions_workout_mmp(VALUE vself, VALUE varray) {
|
|
45
44
|
* ...
|
46
45
|
* slot4 is initialized to 1 + 2 + 3 + 4 + 5
|
47
46
|
*/
|
48
|
-
for (i=1; i<RARRAY_LEN(varray); i++)
|
47
|
+
for (i=1; i<RARRAY_LEN(varray); i++) // loop thru all the numbers
|
49
48
|
maxTot[i] = tot[i] = (tot[i-1] + FIX2INT(RARRAY_PTR(varray)[i])); // compute tot for subsequent cells (starting at cell 0)
|
50
49
|
|
51
|
-
for (offset=1; offset<RARRAY_LEN(varray); offset++) {
|
50
|
+
for (offset=1; offset<RARRAY_LEN(varray); offset++) { // loop starting at index 1 (index-1 is the value to subtract)
|
52
51
|
trail = FIX2INT(RARRAY_PTR(varray)[offset-1]); // this is the value to subtract from the beginning of the list
|
53
|
-
|
54
|
-
|
52
|
+
|
53
|
+
for (i=offset; i<RARRAY_LEN(varray); i++) { // first time subtract 1 from all (except n list)
|
54
|
+
j = i - offset; // subtract trail and add next value
|
55
55
|
tot[j] += (FIX2INT(RARRAY_PTR(varray)[i]) - trail); // to temp variable
|
56
|
-
if (tot[j] > maxTot[j]) maxTot[j] = tot[j]
|
56
|
+
if (tot[j] > maxTot[j]) maxTot[j] = tot[j]; // set maxTot for slot if larger
|
57
57
|
}
|
58
58
|
}
|
59
59
|
|
60
|
-
for (i=0; i<RARRAY_LEN(varray); i++)
|
60
|
+
for (i=0; i<RARRAY_LEN(varray); i++) {
|
61
61
|
rb_ary_push(new_array, INT2FIX(roundNumber((double)maxTot[i] / (double)(i+1))));
|
62
|
+
}
|
62
63
|
|
63
64
|
return new_array;
|
64
65
|
}
|
65
66
|
|
66
67
|
void Init_darvin_functions(){
|
67
68
|
mDarvinFunctions = rb_define_module("DarvinFunctions");
|
68
|
-
rb_define_module_function(mDarvinFunctions, "
|
69
|
+
rb_define_module_function(mDarvinFunctions, "nkmax", mDarvinFunctions_nkmax, 1);
|
69
70
|
}
|
71
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require 'darvin_functions'
|
3
|
+
|
4
|
+
class BenchmarkMMP
|
5
|
+
MAX_POWER = 150000 # 1500.00 Watts as an integer
|
6
|
+
DURATION = 8 # 8 Hours
|
7
|
+
|
8
|
+
def run
|
9
|
+
data = []
|
10
|
+
(DURATION*3600).times do |i|
|
11
|
+
value = Math.sin(i % Math::PI / 2)
|
12
|
+
data << (value * MAX_POWER * 100 * rand).to_i / 100.0
|
13
|
+
end
|
14
|
+
|
15
|
+
tms = Benchmark.bmbm(10) do |x|
|
16
|
+
DURATION.times do |i|
|
17
|
+
x.report("#{i+1} hour(s)") { DarvinFunctions::nkmax(data[0...i*3600]) }
|
18
|
+
end
|
19
|
+
end.last
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/test_mmp.rb
CHANGED
@@ -1,44 +1,27 @@
|
|
1
|
-
require '
|
1
|
+
require 'test/unit'
|
2
2
|
require 'benchmark'
|
3
|
+
require 'darvin_functions'
|
3
4
|
|
4
5
|
class TestMMP < Test::Unit::TestCase
|
5
|
-
MAX_POWER = 150000 # 1500.00 Watts as an integer
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
7
|
+
def test_handle_an_empty_array
|
8
|
+
assert_equal [], DarvinFunctions::nkmax([])
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def test_handle_an_array_of_one
|
12
|
+
assert_equal [10], DarvinFunctions::nkmax([10])
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def test_handle_an_array_of_two
|
16
|
+
assert_equal [10, 7], DarvinFunctions::nkmax([10, 4])
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
19
|
+
def test_handle_an_array_of_three
|
20
|
+
assert_equal [10, 7, 5], DarvinFunctions::nkmax([10, 4, 2])
|
23
21
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
setup do
|
28
|
-
@size = 14400
|
29
|
-
@data = []
|
30
|
-
@size.times do |i|
|
31
|
-
value = Math.sin(i % Math::PI / 2)
|
32
|
-
@data << (value * MAX_POWER * 100 * rand).to_i / 100.0
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
should "run under a minute" do
|
37
|
-
tms = Benchmark.bmbm(10) do |x|
|
38
|
-
x.report { DarvinFunctions::workout_mmp(@data) }
|
39
|
-
end.last
|
40
|
-
assert tms.total < 60, "#{@size} array took too long: #{tms.total} seconds is too long" # seconds
|
41
|
-
end
|
22
|
+
|
23
|
+
def test_handle_an_array_of_three_the_other_way
|
24
|
+
assert_equal [10, 7, 5], DarvinFunctions::nkmax([2, 4, 10])
|
42
25
|
end
|
43
26
|
|
44
27
|
end
|
metadata
CHANGED
@@ -1,78 +1,52 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: darvin_functions
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
12
|
-
- Darvin Bauer
|
7
|
+
authors:
|
13
8
|
- Matt Bauer
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
default_executable:
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
22
|
-
name: shoulda
|
23
|
-
prerelease: false
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
31
|
-
type: :development
|
32
|
-
version_requirements: *id001
|
12
|
+
date: 2012-07-06 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
33
14
|
description: Various functions for data processing, clean up and analysis
|
34
|
-
email:
|
15
|
+
email: bauer@pedalbrain.com
|
35
16
|
executables: []
|
36
|
-
|
37
|
-
extensions:
|
17
|
+
extensions:
|
38
18
|
- ext/extconf.rb
|
39
|
-
extra_rdoc_files:
|
40
|
-
|
41
|
-
files:
|
42
|
-
- ext/darvin_functions.h
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
43
21
|
- ext/darvin_functions.c
|
22
|
+
- ext/darvin_functions.h
|
44
23
|
- ext/extconf.rb
|
45
|
-
-
|
46
|
-
|
47
|
-
homepage: http://github.com/pedalbrain/
|
24
|
+
- test/benchmark_mmp.rb
|
25
|
+
- test/test_mmp.rb
|
26
|
+
homepage: http://github.com/pedalbrain/darvin_functions
|
48
27
|
licenses: []
|
49
|
-
|
50
28
|
post_install_message:
|
51
|
-
rdoc_options:
|
52
|
-
|
53
|
-
require_paths:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
54
31
|
- lib
|
55
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
67
|
-
- 0
|
68
|
-
version: "0"
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
69
44
|
requirements: []
|
70
|
-
|
71
45
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.
|
46
|
+
rubygems_version: 1.8.23
|
73
47
|
signing_key:
|
74
48
|
specification_version: 3
|
75
|
-
summary:
|
76
|
-
test_files:
|
77
|
-
- test/
|
49
|
+
summary: Data Processing C Extenstions
|
50
|
+
test_files:
|
51
|
+
- test/benchmark_mmp.rb
|
78
52
|
- test/test_mmp.rb
|
data/README.rdoc
DELETED
data/test/helper.rb
DELETED