{"id":264901,"date":"2015-09-03T21:36:04","date_gmt":"2015-09-03T17:36:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=264901"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=264901","title":{"rendered":"\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f Go shared library \u043a Ruby"},"content":{"rendered":"<p>             \u0421 \u0432\u044b\u0445\u043e\u0434\u043e\u043c Go 1.5 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c go library \u0434\u043b\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u0422\u043e \u0435\u0441\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c package \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u043b\u0430\u0435\u0442 \u0447\u0442\u043e \u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u0438 \u0442\u044f\u0436\u0435\u043b\u043e \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u043d\u0435 Go \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c C, android, objective C and etc. \u042f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043a Ruby.<br \/>  <a name=\"habracut\"><\/a><br \/>  1. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043d\u0430 go, \u0442\u043e \u043d\u0435 \u0437\u0430\u0447\u0435\u043c \u0435\u0433\u043e \u0437\u0430\u043d\u043e\u0432\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 Ruby;<br \/>  2. Go \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044f\u0432\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 Ruby \u0435\u0441\u043b\u0438 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e \u0441\u0432\u043e\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0430 \u043d\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0441 gem \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 C;<br \/>  3. Go \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0430\u0434\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043a\u0443\u0447\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0439.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 Go package.<\/p>\n<p>  \u041f\u0438\u0448\u0435\u043c \u043d\u0430\u0448\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0435 \u0445\u043e\u0440\u043e\u0448\u043e\u0435 \u0440\u0430\u0431\u043e\u0447\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 Go:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u043e\u0434 \u043d\u0430 Go<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"go\">package main  import &quot;C&quot;  \/\/export add func add(x, y int) int { \tc := 0 \tfor i := 0; i &lt; 50000; i++ { \t\tc += x + y + 1 \t} \treturn c }  func main() {} <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u042d\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c main. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c:   <\/p>\n<pre><code class=\"go\">import &quot;C&quot; <\/code><\/pre>\n<p>  \u0418 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0441 \u043d\u0430\u0440\u0443\u0436\u0438 \u043d\u0430\u0434\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c:  <\/p>\n<pre><code class=\"go\">\/\/export %\u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438% <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0433\u0434\u0430 GO \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0433\u043e\u0442\u043e\u0432\u0430, \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0439 build:  <\/p>\n<pre><code class=\"bash\">go build -buildmode=c-shared -o libadd.so testruby.go <\/code><\/pre>\n<p>  -buildmode \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u043d\u0430 Go 1.5, \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u043d\u0430\u043c \u043d\u0430\u0434\u043e c-shared. \u041f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c .so \u0438 .h \u0444\u0430\u0439\u043b. \u0422\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0438\u0435 \u043d\u0435 GO \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0447\u0430\u0441\u0442\u044c Ruby.<\/p>\n<p>  \u041d\u0430\u043c \u043d\u0443\u0436\u0435\u043d gem <a href=\"https:\/\/github.com\/ffi\/ffi\">ffi<\/a>. \u0421\u0442\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 gem install \u0438\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 gemfile+bundle install. \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043d\u0430\u0448\u0443 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u043a Ruby:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u043e\u0434 \u043d\u0430 Ruby<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\">require 'ffi'  module MegaSum   extend FFI::Library   ffi_lib 'lib\/libadd.so'   attach_function :add, [:int, :int], :int end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0443\u0442 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0433\u0434\u0435 \u043b\u0435\u0436\u0438\u0442 \u043d\u0430\u0448 .so \u0444\u0430\u0439\u043b, \u043a\u0430\u043a\u0438\u0435 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0437\u043e\u0432\u044b (\u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 &quot;\/\/export&quot;), \u0447\u0442\u043e \u043e\u043d\u0438 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 \u0438 \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442(\u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0438\u043f\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/ffi\/ffi\/wiki\/Types\">\u0442\u0443\u0442<\/a>). \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412\u044b\u0437\u043e\u0432 Go<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\"> def self.add_go(x,y)     Sum.add(x,y)   end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 (\u043d\u0430\u0432\u0435\u0440\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c).<\/p>\n<p>  Benchmarks!  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u043e\u0434 \u043d\u0430 Ruby \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u043b\u0430\u0435\u0442 \u0442\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\">def self.add_ruby(x,y)     c = 0     i = 0     while i&lt;50000       c += x + y + 1       i = i+1     end     c   end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<pre><code class=\"bash\">[21] pry(main)&gt; Benchmark.realtime { (1..1000).to_a.each {GoHello.add_ruby(3,2) }} =&gt; 1.763254 [22] pry(main)&gt; Benchmark.realtime { (1..1000).to_a.each {GoHello.add_go(3,2) }} =&gt; 0.030442 [23] pry(main)&gt; Benchmark.realtime { (1..100000).to_a.each {GoHello.add_go(3,2) }} =&gt; 3.103797 [24] pry(main)&gt; Benchmark.realtime { (1..100000).to_a.each {GoHello.add_ruby(3,2) }} =&gt; 195.282368 <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0447\u0442\u043e \u043d\u0430 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 Go \u043e\u0431\u0433\u043e\u043d\u044f\u0435\u0442 Ruby \u0432 60 \u0440\u0430\u0437.<\/p>\n<p>  \u041c\u0438\u043d\u0443\u0441\u044b:<br \/>  1. \u041d\u0435 \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432 Go \u0440\u0430\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u043a\u0443\u0447\u0443 \u0433\u043e\u0440\u0443\u0442\u0438\u043d. \u0423 \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u043d\u0430 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 (\u043d\u0435 \u0442\u044b\u0441\u044f\u0447\u0438 \u0433\u043e\u0440\u0443\u0442\u0438\u043d);<\/p>\n<p>  \u041f.C.: \u0415\u0441\u0442\u044c \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f Python <a href=\"https:\/\/blog.filippo.io\/building-python-modules-with-go-1-5\/\">\u0442\u0443\u0442<\/a>.             <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/266037\/\"> http:\/\/habrahabr.ru\/post\/266037\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>             \u0421 \u0432\u044b\u0445\u043e\u0434\u043e\u043c Go 1.5 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c go library \u0434\u043b\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u0422\u043e \u0435\u0441\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c package \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u043b\u0430\u0435\u0442 \u0447\u0442\u043e \u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u0438 \u0442\u044f\u0436\u0435\u043b\u043e \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u043d\u0435 Go \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c C, android, objective C and etc. \u042f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043a Ruby.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-264901","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/264901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264901"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/264901\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}