This file contains 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 <dispatch/dispatch.h> | |
#include <ruby/ruby.h> | |
VALUE primes; | |
static VALUE primes_new(VALUE self) | |
{ | |
return self; | |
} | |
// MY LATEST ATTEMPT USING GRAND CENTRAL | |
static VALUE primes_generate(VALUE self) | |
{ | |
int i; | |
__block VALUE final; | |
__block int j; | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_queue_t private_queue = dispatch_queue_create("com.ruby.queue", NULL); | |
dispatch_group_t group = dispatch_group_create(); | |
for ( i = 2; i < 100000; i++ ) { | |
// dispatch_retain(private_queue); | |
dispatch_group_async(group, queue, ^{ | |
for ( j = 2; j <= i/2; j++ ) { | |
if ( ! ( i % j ) ) break; | |
} | |
if ( j > i / 2 ) { | |
dispatch_sync(private_queue, ^{ final = INT2FIX(i); }); | |
// dispatch_release(private_queue); | |
} | |
}); | |
} | |
dispatch_group_wait(group, DISPATCH_TIME_FOREVER); | |
return final; | |
} | |
// THE ORIGINAL VANILLA RUBY METHOD | |
// static VALUE primes_generate(VALUE self) | |
// { | |
// VALUE final = INT2FIX(0); | |
// int i, j; | |
// | |
// for ( i = 2; i < 100000; i++ ) | |
// { | |
// for ( j = 2; j <= i/2; j++ ) | |
// { | |
// if ( ! ( i % j ) ) break; | |
// } | |
// | |
// if ( j > i / 2 ) { | |
// final = INT2FIX(i); | |
// } | |
// } | |
// return final; | |
// } | |
void Init_primes() | |
{ | |
primes = rb_define_class("Primes", rb_cObject); | |
rb_define_method(primes, "intialize", primes_new, 0); | |
rb_define_method(primes, "generate", primes_generate, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment