Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
reu committed Apr 13, 2012
0 parents commit f310181
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
@@ -0,0 +1,19 @@
*.gem
*.rbc
*.bundle
*.o
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in standard_deviation.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 Rodrigo Navarro

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 changes: 60 additions & 0 deletions README.md
@@ -0,0 +1,60 @@
# Native Standard Deviation

An implementation of the standard deviation calculation in C, with much better performance (50x-100x) than using pure ruby.

## Installation

Add this line to your application's Gemfile:

gem 'standard_deviation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install standard_deviation

## Usage

Just call `standard_deviation` or `stdev` on a array with numbers.

``` ruby
[1, 2, 3, 4, 5].stdev => 1.5811388300841898

[521.0, BigDecimal("1234.45"), 1_120].standard_deviation => 383.168958598336
```

## Benchmarks

``` ruby
# Basic ruby implementation
class Array
def stdev_ruby
total = inject :+
mean = total.to_f / size
variance = inject(0) { |variance, value| variance + (value - mean) ** 2 }

Math.sqrt(variance / (size - 1))
end
end

# Running on a 2.4 GHz MacBook Pro
n = 10000
values = (1..10_000).map(&:to_f)

Benchmark.bm do |b|
b.report do
n.times { values.stdev }
end

b.report do
n.times { values.stdev_ruby }
end
end

# user system total real
# 1.070000 0.000000 1.070000 ( 1.065246)
# 88.180000 0.550000 88.730000 ( 93.835144)
```
19 changes: 19 additions & 0 deletions Rakefile
@@ -0,0 +1,19 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rake/extensiontask"
require "rspec"
require "rspec/core/rake_task"

Rake::ExtensionTask.new("standard_deviation") do |ext|
ext.lib_dir = File.join "lib", "standard_deviation"
end


RSpec::Core::RakeTask.new("spec") do |t|
t.verbose = true
end

Rake::Task[:spec].prerequisites << :compile

task :test => :spec
task :default => :test
2 changes: 2 additions & 0 deletions ext/standard_deviation/extconf.rb
@@ -0,0 +1,2 @@
require "mkmf"
create_makefile "standard_deviation/standard_deviation"
28 changes: 28 additions & 0 deletions ext/standard_deviation/standard_deviation.c
@@ -0,0 +1,28 @@
#include <ruby.h>
#include <math.h>

static VALUE stdev(VALUE self) {
int i, size;
double total, mean, variance;

size = RARRAY_LEN(self);
total = variance = 0;
VALUE *array = RARRAY_PTR(self);

for (i = 0; i < size; i++) {
total += NUM2DBL(array[i]);
}

mean = total / size;

for (i = 0; i < size; i++) {
variance += pow((NUM2DBL(array[i]) - mean), 2);
}

return rb_float_new(sqrt(variance / (size - 1)));
}

void Init_standard_deviation() {
rb_define_method(rb_cArray, "stdev", stdev, 0);
rb_define_alias(rb_cArray, "standard_deviation", "stdev");
}
5 changes: 5 additions & 0 deletions lib/standard_deviation.rb
@@ -0,0 +1,5 @@
require "standard_deviation/version"
require "standard_deviation/standard_deviation"

module StandardDeviation
end
3 changes: 3 additions & 0 deletions lib/standard_deviation/version.rb
@@ -0,0 +1,3 @@
module StandardDeviation
VERSION = "0.0.1"
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,2 @@
require "standard_deviation"
require "bigdecimal"
22 changes: 22 additions & 0 deletions spec/standard_deviation/standard_deviation_spec.rb
@@ -0,0 +1,22 @@
require "spec_helper"

describe Array do
describe "#stdev" do
subject { values.stdev }

context "with integer values" do
let(:values) { [1, 2, 6, 3, -4, 23] }
it { should == 9.32559202767667 }
end

context "with float values" do
let(:values) { [1.0, 2.0, 6.0, 3.0, -4.0, 23.0] }
it { should == 9.32559202767667 }
end

context "with bigdecimal values" do
let(:values) { [BigDecimal("1.0"), BigDecimal("2.0"), BigDecimal("6.0"), BigDecimal("3.0"), BigDecimal("-4.0"), BigDecimal("23.0")] }
it { should == 9.32559202767667 }
end
end
end
22 changes: 22 additions & 0 deletions standard_deviation.gemspec
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/standard_deviation/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Rodrigo Navarro"]
gem.email = ["rnavarro1@gmail.com"]
gem.description = %q{An implementation of the standard deviation calculation in C,
with much better performance (50x-100x) than using pure ruby.}
gem.summary = %q{Native extension for Ruby to calculate standard deviation.}
gem.homepage = "https://github.com/reu/standard_deviation"

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.extensions = ["ext/standard_deviation/extconf.rb"]
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "standard_deviation"
gem.require_paths = ["lib", "ext"]
gem.version = StandardDeviation::VERSION

gem.add_development_dependency "rake-compiler"
gem.add_development_dependency "rspec"
end

0 comments on commit f310181

Please sign in to comment.