Skip to content

Instantly share code, notes, and snippets.

@bmnick
Created March 14, 2011 01:37
Show Gist options
  • Select an option

  • Save bmnick/868641 to your computer and use it in GitHub Desktop.

Select an option

Save bmnick/868641 to your computer and use it in GitHub Desktop.
#include "ruby.h"
VALUE CExt = Qnil;
VALUE method_floatmul(VALUE self);
VALUE method_loop_floatmul(VALUE self);
void Init_CExt() {
CExt = rb_define_module("CExt");
rb_define_singleton_method(CExt, "floatmul", method_floatmul, 0);
rb_define_singleton_method(CExt, "loop_floatmul", method_loop_floatmul, 0);
}
VALUE method_floatmul(VALUE self) {
double a = 3.0f, b = 2.0f, c;
c = a * b;
return rb_float_new(c);
}
VALUE method_loop_floatmul(VALUE self) {
double a = 3.0f, b = 2.0f, c;
int i;
for (i = 0; i < 1000000; i++) {
c = a * b;
}
return rb_float_new(c);
}
require 'mkmf'
extension_name = 'CExt'
dir_config = extension_name
create_makefile(extension_name)
require_relative 'cext/CExt'
CExt.loop_floatmul
require_relative 'cext/CExt'
1000000.times do
CExt.floatmul
end
int main() {
double a = 3.0f, b = 2.0f, c;
int i;
for (i = 0; i < 1000000; i++) {
c = a * b;
}
return 0;
}
a, b = 3.0, 2.0
c = 0.0
1000000.times do
c = a * b
end
time ruby pure.rb # ruby pure.rb 0.18s user 0.01s system 92% cpu 0.203 total
time ruby mixed.rb # ruby mixed.rb 0.20s user 0.01s system 92% cpu 0.230 total
time ruby external.sh # ruby external.rb 0.01s user 0.01s system 60% cpu 0.025 total
time purec # ./purec 0.00s user 0.00s system 48% cpu 0.011 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment