Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Ruby Time objects from C extension

I am writing a C extension for Ruby, which needs to heavily use time objects. The performance is critical for this application. How should I go about creating Time objects from C API for maximum performance? I did not find anything relevant in ruby.h include file.

like image 243
Piotr Turek Avatar asked Jun 30 '26 13:06

Piotr Turek


1 Answers

I've always just used rb_time_new - it takes a time_t and a second parameter with the fractional part of the time, in microseconds.

Depending on what you are doing it might make sense to only store the time_t (and do your calculations on those) and lazily convert to ruby Time objects only when you have to

like image 59
Frederick Cheung Avatar answered Jul 02 '26 10:07

Frederick Cheung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!