{"id":121142,"date":"2024-04-26T10:28:21","date_gmt":"2024-04-26T01:28:21","guid":{"rendered":"https:\/\/softantenna.com\/blog\/?p=121142"},"modified":"2024-04-26T10:28:21","modified_gmt":"2024-04-26T01:28:21","slug":"ruby-faster-than-you-think","status":"publish","type":"post","link":"https:\/\/softantenna.com\/blog\/ruby-faster-than-you-think\/","title":{"rendered":"Ruby\u306f\u601d\u3063\u305f\u3088\u308a\u901f\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u3068\u306e\u6307\u6458"},"content":{"rendered":"<p><img decoding=\"async\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/softantenna.com\/blog\/wp-content\/uploads\/2024\/04\/pexels-photo-399636.jpeg\" alt=\"Pexels photo 399636\" title=\"pexels-photo-399636.jpeg\" border=\"0\" width=\"1128\" height=\"750\" \/><\/p>\n<p>\u56fd\u7523\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u300c<a href=\"https:\/\/www.ruby-lang.org\/ja\/\">Ruby<\/a>\u300d\u306f\u3001\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u578b\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u51e6\u7406\u901f\u5ea6\u304c\u9045\u305d\u3046\u300d\u3068\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u3092\u6301\u3064\u958b\u767a\u8005\u306e\u65b9\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u3057\u304b\u3057\u4eca\u56de\u3001Ruby\u3067\u66f8\u304b\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3082\u5de5\u592b\u6b21\u7b2c\u3067\u5341\u5206\u9ad8\u901f\u306b\u64cd\u4f5c\u3059\u308b\u3053\u3068\u3092\u3001John Hawthorn\u6c0f\u306f\u30d6\u30ed\u30b0\u8a18\u4e8b\u300c<a href=\"https:\/\/www.johnhawthorn.com\/2024\/ruby-might-be-faster-than-you-think\/\">Ruby might be faster than you think<\/a>\u300d\u3067\u6307\u6458\u3057\u3001<a href=\"https:\/\/news.ycombinator.com\/item?id=40152029\">Hacker News<\/a>\u3084<a href=\"https:\/\/www.reddit.com\/r\/programming\/comments\/1cd5biq\/ruby_might_be_faster_than_you_think\/\">Reddit<\/a>\u3067\u6ce8\u76ee\u3092\u96c6\u3081\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u540c\u6c0f\u304c\u4f8b\u3068\u3057\u3066\u63b2\u8f09\u3057\u3066\u3044\u308b\u306e\u306f\u3001<a href=\"https:\/\/github.com\/wouterken\/crystalruby\">crystalruby<\/a>\u3068\u547c\u3070\u308c\u308b\u3001Ruby\u306e\u4e2d\u304b\u3089Crystal\u3092\u547c\u3073\u51fa\u3059\u305f\u3081\u306egem\u306e\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3059\u3002<\/p>\n<p class=\"is-style-icon_info\">\nCrystal\u306fRuby\u306b\u4f3c\u305f\u69cb\u6587\u3092\u63a1\u7528\u3057\u305f\u9759\u7684\u578b\u4ed8\u3051\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3059\u3002\n<\/p>\n<p>\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u3092\u8a08\u7b97\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3001Pure Ruby\u7248\u306e<code>fib_rb<\/code>\u3068Crystal\u306e\u30b3\u30fc\u30c9\u3092\u547c\u3073\u51fa\u3059<code>fib_cr<\/code>\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>require 'crystalruby'\nrequire 'benchmark'\n\nmodule Fibonnaci\n  crystalize [n: :int32] => :int32\n  def fib_cr(n)\n    a = 0\n    b = 1\n    n.times { a, b = b, a + b }\n    a\n  end\n\n  module_function\n\n  def fib_rb(n)\n    a = 0\n    b = 1\n    n.times { a, b = b, a + b }\n    a\n  end\nend\n\nputs(Benchmark.realtime { 1_000_000.times { Fibonnaci.fib_rb(30) } })\nputs(Benchmark.realtime { 1_000_000.times { Fibonnaci.fib_cr(30) } })<\/code><\/pre>\n<\/div>\n<p>\u6700\u521d\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001Ruby\u7248\u304c\u5927\u5e45\u306b\u9045\u3044\u7d50\u679c\u3068\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre>\n$ ruby test.rb\n2.113752398872748\n0.6535678738728166\n<\/pre>\n<p>\u3057\u304b\u3057\u540c\u6c0f\u306f\u3001Ruby\u306e\u30d6\u30ed\u30c3\u30af\u304c\u914d\u5217\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u306b\u7740\u76ee\u3057\u3001\u3053\u308c\u3092\u6291\u5236\u3059\u308b\u3053\u3068\u3067\u901f\u5ea6\u3092\u6539\u5584\u3057\u307e\u3059(n.times{}\u306e\u6700\u5f8c\u3067nil\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059)\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>def fib_rb(n)\n  a = 0\n  b = 1\n  n.times { a, b = b, a + b; nil }\n  a\nend<\/code><\/pre>\n<\/div>\n<p>\u3055\u3089\u306b\u30d6\u30ed\u30c3\u30af\u3092\u666e\u901a\u306e\u30eb\u30fc\u30d7\u306b\u66f8\u304d\u63db\u3048\u3001\u6700\u8fd1\u306eRuby\u3067\u5229\u7528\u53ef\u80fd\u306aYJIT\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067Pure Ruby\u7248\u304cCrystal\u7248\u306e\u901f\u5ea6\u3092\u4e0a\u56de\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>def fib_rb(n)\n  a = 0\n  b = 1\n  while n > 0\n    a, b = b, a + b\n    n -= 1\n  end\n  a\nend<\/code><\/pre>\n<\/div>\n<p><code>ruby --yjit<\/code>\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre>\n$ ruby --yjit test.rb\n0.10502525512129068\n0.5002881051041186\n<\/pre>\n<p>\u6700\u7d42\u7684\u306bCrystal\u7248\u3088\u308a5\u500d\u9ad8\u901f\u5ea6\u3067\u3001\u30aa\u30ea\u30b8\u30ca\u30eb\u7248\u306eRuby\u3088\u308a20\u500d\u901f\u3044\u7d50\u679c\u304c\u3067\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u540c\u6c0f\u306f\u3001FFI(Foreign function interface)\u304b\u3001\u305d\u308c\u306b\u985e\u3059\u308b\u3082\u306e\u306b\u3088\u308b\u30b3\u30b9\u30c8\u304c\u5f71\u97ff\u3057\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304c\u3001Ruby\u306e\u30b3\u30fc\u30c9\u306b\u5c11\u3057\u624b\u3092\u52a0\u3048\u308b\u3060\u3051\u3067\u3001\u5927\u304d\u304f\u901f\u5ea6\u304c\u5411\u4e0a\u3059\u308b\u306e\u306f\u6ce8\u76ee\u306b\u5024\u3059\u308b\u3068\u307e\u3068\u3081\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u500b\u4eba\u7684\u306b\u306f\u6700\u7d42\u7684\u306a\u52dd\u6557\u3088\u308a\u3082\u3001\u30d6\u30ed\u30c3\u30af\u3092\u30eb\u30fc\u30d7\u306b\u5c55\u958b\u3059\u308b\u3053\u3068\u3067\u901f\u5ea6\u304c\u5411\u4e0a\u3059\u308b\u3068\u3053\u308d\u304c\u8208\u5473\u6df1\u3044\u3068\u601d\u3044\u307e\u3057\u305f\u3002Ruby\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u826f\u3057\u305f\u3044\u5834\u5408\u306b\u53c2\u8003\u306b\u306a\u308b\u30c6\u30af\u30cb\u30c3\u30af\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<table>\n<tr>\n<th rowspan='4' style='white-space: nowrap;'><img decoding=\"async\" style=\"max-width: 52px; height: auto;\" src=\"https:\/\/softantenna.com\/uploads\/software\/1964\/s_icon(2).png\" \/><\/th>\n<th style='white-space: nowrap;'>\u30bf\u30a4\u30c8\u30eb<\/th>\n<td>Ruby<\/td>\n<\/tr>\n<tr>\n<th style='white-space: nowrap;'>\u516c\u5f0f\u30b5\u30a4\u30c8<\/th>\n<td><a href=\"http:\/\/www.ruby-lang.org\/ja\/\">http:\/\/www.ruby-lang.org\/ja\/<\/a><\/td>\n<\/tr>\n<tr>\n<th style='white-space: nowrap;'>\u30bd\u30d5\u30c8\u30a2\u30f3\u30c6\u30ca<\/th>\n<td><a href=\"https:\/\/softantenna.com\/softwares\/1964-ruby\">https:\/\/softantenna.com\/softwares\/1964-ruby<\/a><\/td>\n<\/tr>\n<tr>\n<th style='white-space: nowrap;'>\u8aac\u660e<\/th>\n<td>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30b9\u30af\u30ea\u30d7\u30c8\u8a00\u8a9e\u3002<\/td>\n<\/tr>\n<\/table>\n<table>\n<tr>\n<th rowspan='4' style='white-space: nowrap;'><img decoding=\"async\" style=\"max-width: 52px; height: auto;\" src=\"https:\/\/softantenna.com\/uploads\/software\/6966\/s_icon.png\" \/><\/th>\n<th style='white-space: nowrap;'>\u30bf\u30a4\u30c8\u30eb<\/th>\n<td>Crystal<\/td>\n<\/tr>\n<tr>\n<th style='white-space: nowrap;'>\u516c\u5f0f\u30b5\u30a4\u30c8<\/th>\n<td><a href=\"https:\/\/crystal-lang.org\/\">https:\/\/crystal-lang.org\/<\/a><\/td>\n<\/tr>\n<tr>\n<th style='white-space: nowrap;'>\u30bd\u30d5\u30c8\u30a2\u30f3\u30c6\u30ca<\/th>\n<td><a href=\"https:\/\/softantenna.com\/softwares\/6966-crystal\">https:\/\/softantenna.com\/softwares\/6966-crystal<\/a><\/td>\n<\/tr>\n<tr>\n<th style='white-space: nowrap;'>\u8aac\u660e<\/th>\n<td>Ruby\u98a8\u306e\u30b7\u30f3\u30bf\u30c3\u30af\u30b9\uff06\u9759\u7684\u306a\u578b\u3092\u6301\u3064\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u56fd\u7523\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u300cRuby\u300d\u306f\u3001\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u578b\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u3042\u308b\u305f\u3081\u3001\u300c\u51e6\u7406\u901f\u5ea6\u304c\u9045\u305d\u3046\u300d\u3068\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u3092\u6301\u3064\u958b\u767a\u8005\u306e\u65b9\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002 \u3057\u304b\u3057\u4eca\u56de\u3001Ruby\u3067\u66f8\u304b\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3082\u5de5\u592b\u6b21\u7b2c\u3067\u5341\u5206\u9ad8\u901f\u306b [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":121143,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[75],"tags":[154],"class_list":["post-121142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-ruby"],"_links":{"self":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/posts\/121142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/comments?post=121142"}],"version-history":[{"count":0,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/posts\/121142\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/media\/121143"}],"wp:attachment":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/media?parent=121142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/categories?post=121142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/tags?post=121142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}