Created
March 14, 2011 01:37
-
-
Save bmnick/868641 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'mkmf' | |
| extension_name = 'CExt' | |
| dir_config = extension_name | |
| create_makefile(extension_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative 'cext/CExt' | |
| CExt.loop_floatmul |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative 'cext/CExt' | |
| 1000000.times do | |
| CExt.floatmul | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int main() { | |
| double a = 3.0f, b = 2.0f, c; | |
| int i; | |
| for (i = 0; i < 1000000; i++) { | |
| c = a * b; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a, b = 3.0, 2.0 | |
| c = 0.0 | |
| 1000000.times do | |
| c = a * b | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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