# How to speed up ruby and make it as fast as possible

**URL:** <https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965>\
**Category:** ruby-talk\
**Created:** [14 July 2006 14:47 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965 "2006-07-14T14:47:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamal\_Mazrui](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jamal\_Mazrui](https://rubytalk.org/u/Jamal_Mazrui)\
**Post date:** [14 July 2006 14:47 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/1 "2006-07-14T14:47:50Z")

</div>

> Write it C.

I find this answer to be unsatisfactory. I do not know C and neither do  
many others who like Ruby but want better performance. The knowledge  
and syntax demands of programming in Ruby and C are worlds apart. It is  
not much different than telling someone to go code in (modern)  
Assembler!

I think the more that Rubyists acknowledge and try to directly solve the  
language's performance hindrance rather than rationalizing it away, the  
better the prospects for the success of our beloved language. Of  
course, better algorithms can produce better performance, but that  
argument is also true for the languages with which Ruby compares  
unfavorably in performance. Please do not claim that Ruby's adoption by  
others is irrelevant: a good idea is worth spreading, and the more  
users, the better library support and learning resources available to us  
all.  
Also, please do not tell me to rewrite Ruby, myself, if I care about  
performance. As stated, I do not know C or have the interest and time  
to devote to such an undertaking. I do promote Ruby to friends and  
colleagues in ways that I can. We should be able to have a legitimate  
desire for language improvements without being characterized as  
ignoramuses, nay-sayers or lazy spectators.

Regards,  
Jamal

> **···**
>
> -----Original Message-----  
> From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf  
> Of Reggie Mr  
> Sent: Friday, July 14, 2006 6:56 AM

---

<div class="post-metadata">

**Author:** ![Kent\_Sibilev1](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kent\_Sibilev1](https://rubytalk.org/u/Kent_Sibilev1)\
**Post date:** [14 July 2006 15:12 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/2 "2006-07-14T15:12:49Z")

</div>

I find this attitude is quite amazing. We all know that Ruby is not a  
best language in terms of performance. Even if we had Ruby 2.0 (YARV)  
delivered to us, Ruby's performance would still be a way behind those  
strong typed languges like ocaml or java. If you want to keep using  
Ruby, but sometimes you need a raw performance, you should learn C.  
It's not that bad, you know. If you always need a raw performance,  
then probably you are not using right tool for the job.

just my 2 cents.

> **···**
>
> On 7/14/06, Jamal Mazrui \<Jamal.Mazrui@fcc.gov\> wrote:
> 
> > -----Original Message-----  
> > From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf  
> > Of Reggie Mr  
> > Sent: Friday, July 14, 2006 6:56 AM
> > 
> > \> Write it C.
> > 
> > I find this answer to be unsatisfactory. I do not know C and neither do  
> > many others who like Ruby but want better performance. The knowledge  
> > and syntax demands of programming in Ruby and C are worlds apart. It is  
> > not much different than telling someone to go code in (modern)  
> > Assembler!
> > 
> > I think the more that Rubyists acknowledge and try to directly solve the  
> > language's performance hindrance rather than rationalizing it away, the  
> > better the prospects for the success of our beloved language. Of  
> > course, better algorithms can produce better performance, but that  
> > argument is also true for the languages with which Ruby compares  
> > unfavorably in performance. Please do not claim that Ruby's adoption by  
> > others is irrelevant: a good idea is worth spreading, and the more  
> > users, the better library support and learning resources available to us  
> > all.  
> > Also, please do not tell me to rewrite Ruby, myself, if I care about  
> > performance. As stated, I do not know C or have the interest and time  
> > to devote to such an undertaking. I do promote Ruby to friends and  
> > colleagues in ways that I can. We should be able to have a legitimate  
> > desire for language improvements without being characterized as  
> > ignoramuses, nay-sayers or lazy spectators.
> > 
> > Regards,  
> > Jamal
> 
> --  
> Kent  
> ---
> 
> > **[DATANOISE.COM](https://www.datanoise.com)**
> >
> > DATANOISE.COM - Contact us for any business inquiries

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [14 July 2006 15:24 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/3 "2006-07-14T15:24:03Z")

</div>

We all want Ruby to be faster but if the speed at which your application runs at is your priority then I'm afraid that Ruby is not good enough. Neither is Python, Perl, Java or any other language. In extremis even C isn't good enough and only assembler will do.

Please do not sell Ruby as the Golden Hammer that can do anything better than any other language, it quite clearly cannot do that. You will do Ruby a disservice and people will question your ability to make a sound technical judgement.

Of course there are many things that you can do to make a Ruby program faster, like writing better code 🙂

A programming language is simply a tool and not everything is a nail.

---

<div class="post-metadata">

**Author:** ![gregarican](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@gregarican](https://rubytalk.org/u/gregarican)\
**Post date:** [14 July 2006 15:45 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/4 "2006-07-14T15:45:10Z")

</div>

I prefer to use Ruby for \_most\_ of my programming work, but still  
couldn't imagine using it for every single task. It's just one tool of  
many. And the more of these tools you can pick up and use that much  
better of a craftsman you will be.

That being said, I would strongly recommend you do learn C. The Ruby  
interpreter relies on it, Ruby has a very clean, logical interface with  
it, and learning it would allow you to tap into a whole other world of  
available libraries. Just review some of Pickaxe for basic details  
about how it can help make your life easier, not more complex. Plus, as  
others have mentioned, learning it would allow to use it for  
performance critical portions of your work.

Bad analogy maybe, but being a programmer and not knowing any C is like  
a guitarist now knowing any Led Zeppelin 🙂

Jamal Mazrui wrote:

> **···**
>
> > -----Original Message-----  
> > From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf  
> > Of Reggie Mr  
> > Sent: Friday, July 14, 2006 6:56 AM
> > 
> > \> Write it C.
> > 
> > I find this answer to be unsatisfactory. I do not know C and neither do  
> > many others who like Ruby but want better performance. The knowledge  
> > and syntax demands of programming in Ruby and C are worlds apart. It is  
> > not much different than telling someone to go code in (modern)  
> > Assembler!
> > 
> > I think the more that Rubyists acknowledge and try to directly solve the  
> > language's performance hindrance rather than rationalizing it away, the  
> > better the prospects for the success of our beloved language. Of  
> > course, better algorithms can produce better performance, but that  
> > argument is also true for the languages with which Ruby compares  
> > unfavorably in performance. Please do not claim that Ruby's adoption by  
> > others is irrelevant: a good idea is worth spreading, and the more  
> > users, the better library support and learning resources available to us  
> > all.  
> > Also, please do not tell me to rewrite Ruby, myself, if I care about  
> > performance. As stated, I do not know C or have the interest and time  
> > to devote to such an undertaking. I do promote Ruby to friends and  
> > colleagues in ways that I can. We should be able to have a legitimate  
> > desire for language improvements without being characterized as  
> > ignoramuses, nay-sayers or lazy spectators.
> > 
> > Regards,  
> > Jamal

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [14 July 2006 17:50 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/5 "2006-07-14T17:50:16Z")

</div>

Jamal Mazrui wrote:

> From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf  
> Of Reggie Mr  
> Sent: Friday, July 14, 2006 6:56 AM
> 
> > Write it C.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > I find this answer to be unsatisfactory. I do not know C and neither do  
> > many others who like Ruby but want better performance. The knowledge  
> > and syntax demands of programming in Ruby and C are worlds apart. It is  
> > not much different than telling someone to go code in (modern)  
> > Assembler!
> 
> I think the more that Rubyists acknowledge and try to directly solve the  
> language's performance hindrance rather than rationalizing it away, the  
> better the prospects for the success of our beloved language. [snip]  
> &nbsp;&nbsp;

I don't think people are ignoring Ruby's performance or rationalizing it away. That's why all that work is going into YARV for Ruby2. That's why there is ruby2c. There are people who are consciously working towards a better performing Ruby. As far as I understand it, there is plenty of room for Ruby to improve (performance has not been a top priority previously), so I have no doubt that we will see a faster Ruby in the future.

However, if you want an immediate solution, then the solution is to use another language or link to another language from Ruby. The helpful people on this list are also very practical, so when someone says, "How do I make my Ruby program run faster?" instead of answering, "Sorry, you can't" they give practical advice.

Sorry to be blunt, but if you are not contributing to improving Ruby's performance, then you (and I 🙂 will need to be patient and wait for the newer, faster, even more awesome versions of Ruby to come out.

-Justin

> **···**
>
> > -----Original Message-----

---

<div class="post-metadata">

**Author:** ![Ryan\_McGovern](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Ryan\_McGovern](https://rubytalk.org/u/Ryan_McGovern)\
**Post date:** [14 July 2006 15:27 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/6 "2006-07-14T15:27:56Z")

</div>

"I find this answer to be unsatisfactory. I do not know C and neither do  
many others who like Ruby but want better performance. The knowledge  
and syntax demands of programming in Ruby and C are worlds apart. It is  
not much different than telling someone to go code in (modern)  
Assembler! "

Assembler is not that bad either, imho once you've learned how to program beyond just a specific language picking up another one is not that challenging. And i agree learn c it's at the very least good for very speed important tasks, or interfacing directly with the os or writing ruby modules to do said interfacing.

I mean try to beat this thing in speed \*need an x86 compiler\*

.model small

.stack 100h

.386

Bufsize= 800

bsize=800

.data

ten DD 10

FACT DD Bufsize DUP(0)

INDEX dd 0

MULTI DD 0

LEN DD 1

.code

main proc far

&nbsp;&nbsp;&nbsp;&nbsp;mov ax,@data

&nbsp;&nbsp;&nbsp;&nbsp;mov ds,ax

&nbsp;&nbsp;&nbsp;&nbsp;mov ah,62h

&nbsp;&nbsp;&nbsp;&nbsp;int 21h

&nbsp;&nbsp;&nbsp;&nbsp;mov es,bx

&nbsp;&nbsp;&nbsp;&nbsp;mov eax,0

&nbsp;&nbsp;&nbsp;&nbsp;mov ebx,081h

&nbsp;&nbsp;&nbsp;&nbsp;movzx ecx,BYTE PTR es:[80h]

L4: mul ten

&nbsp;&nbsp;&nbsp;&nbsp;movzx edx,BYTE PTR es:[ebx]

&nbsp;&nbsp;&nbsp;&nbsp;and edx,000000fh

&nbsp;&nbsp;&nbsp;&nbsp;add eax,edx

&nbsp;&nbsp;&nbsp;&nbsp;inc ebx

&nbsp;&nbsp;&nbsp;&nbsp;loop L4

;put code here

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV ESI,OFFSET FACT

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV [ESI],EAX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEC EAX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV ECX,EAX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CMP ECX,0

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JE PEAX

Fac1:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV EBX,0

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV ESI,OFFSET FACT

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV INDEX,0

Fac2:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV EAX,[ESI] ;MOV FACT INTO EAX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MUL ECX ;MUL BY COUNT

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADD EAX,EBX ;ADD CONTENTS OF EBX TO ECX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JNC Fac3 ;NO CARRY =\> JUMP L3

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INC EDX ;OTHERWISE INC EDX

Fac3:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV EBX,EDX ;MOV CONTENT OF EDX TO EBX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV [ESI],EAX ;STORE MUL RESULT IN CURRENT ESI POS

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INC INDEX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV EAX,INDEX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CMP LEN,EAX ;COMPARE LEN, INDEX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JNE Fac5 ;NOT EQUAL =\> JUMP L2

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CMP EBX,0 ;COMPARE FOR CARRY AFTER ADD ECX,EBX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JZ Fac4 ;NO CARRY =\> JUMP L4

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INC LEN ;INC LEN

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADD ESI,4

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MOV [ESI],EBX

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JMP Fac4

Fac5:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADD ESI,4

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JMP Fac2

Fac4:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOOP Fac1

mov INDEX,0

mov ESI,OFFSET FACT

MK1:

mov eax,[esi]

push eax

inc INDEX

add esi,4

mov ecx,INDEX

cmp ecx, LEN

jnz MK1

;end code

PEAX:

&nbsp;&nbsp;&nbsp;&nbsp;pop eax

&nbsp;&nbsp;&nbsp;&nbsp;mov ebx,eax

&nbsp;&nbsp;&nbsp;&nbsp;mov ecx,8

&nbsp;&nbsp;&nbsp;&nbsp;cmp INDEX,0

&nbsp;&nbsp;&nbsp;&nbsp;jz I1

L9:

L3:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov edx,0f0000000h

&nbsp;&nbsp;&nbsp;&nbsp;and edx,ebx

&nbsp;&nbsp;&nbsp;&nbsp;push ecx

&nbsp;&nbsp;&nbsp;&nbsp;mov cl,28

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shr edx,cl

&nbsp;&nbsp;&nbsp;&nbsp;pop ecx

&nbsp;&nbsp;&nbsp;&nbsp;mov ah,02h

&nbsp;&nbsp;&nbsp;&nbsp;cmp dl,10

&nbsp;&nbsp;&nbsp;&nbsp;jb L1

&nbsp;&nbsp;&nbsp;&nbsp;add dl,37h

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jmp L2

L1: or dl,30h

L2: int 21h

&nbsp;&nbsp;&nbsp;&nbsp;shl ebx,1

&nbsp;&nbsp;&nbsp;&nbsp;shl ebx,1

&nbsp;&nbsp;&nbsp;&nbsp;shl ebx,1

&nbsp;&nbsp;&nbsp;&nbsp;shl ebx,1

&nbsp;&nbsp;&nbsp;&nbsp;loop L3

&nbsp;&nbsp;&nbsp;&nbsp;dec INDEX

&nbsp;&nbsp;&nbsp;&nbsp;cmp INDEX,0

&nbsp;&nbsp;&nbsp;&nbsp;jnz PEAX

&nbsp;&nbsp;&nbsp;&nbsp;mov ah,4ch

&nbsp;&nbsp;&nbsp;&nbsp;int 21h

I1:

&nbsp;&nbsp;&nbsp;&nbsp;inc INDEX

&nbsp;&nbsp;&nbsp;&nbsp;jmp L9

main endp

end main

Kent Sibilev wrote:

> **···**
>
> > I find this attitude is quite amazing. We all know that Ruby is not a  
> > best language in terms of performance. Even if we had Ruby 2.0 (YARV)  
> > delivered to us, Ruby's performance would still be a way behind those  
> > strong typed languges like ocaml or java. If you want to keep using  
> > Ruby, but sometimes you need a raw performance, you should learn C.  
> > It's not that bad, you know. If you always need a raw performance,  
> > then probably you are not using right tool for the job.
> > 
> > just my 2 cents.
> > 
> > On 7/14/06, Jamal Mazrui \<Jamal.Mazrui@fcc.gov\> wrote:
> > 
> > > -----Original Message-----  
> > > From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf  
> > > Of Reggie Mr  
> > > Sent: Friday, July 14, 2006 6:56 AM
> > > 
> > > \> Write it C.
> > > 
> > > I find this answer to be unsatisfactory. I do not know C and neither do  
> > > many others who like Ruby but want better performance. The knowledge  
> > > and syntax demands of programming in Ruby and C are worlds apart. It is  
> > > not much different than telling someone to go code in (modern)  
> > > Assembler!
> > > 
> > > I think the more that Rubyists acknowledge and try to directly solve the  
> > > language's performance hindrance rather than rationalizing it away, the  
> > > better the prospects for the success of our beloved language. Of  
> > > course, better algorithms can produce better performance, but that  
> > > argument is also true for the languages with which Ruby compares  
> > > unfavorably in performance. Please do not claim that Ruby's adoption by  
> > > others is irrelevant: a good idea is worth spreading, and the more  
> > > users, the better library support and learning resources available to us  
> > > all.  
> > > Also, please do not tell me to rewrite Ruby, myself, if I care about  
> > > performance. As stated, I do not know C or have the interest and time  
> > > to devote to such an undertaking. I do promote Ruby to friends and  
> > > colleagues in ways that I can. We should be able to have a legitimate  
> > > desire for language improvements without being characterized as  
> > > ignoramuses, nay-sayers or lazy spectators.
> > > 
> > > Regards,  
> > > Jamal

---

<div class="post-metadata">

**Author:** ![Jeremy\_Henty](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@Jeremy\_Henty](https://rubytalk.org/u/Jeremy_Henty)\
**Post date:** [14 July 2006 21:40 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/7 "2006-07-14T21:40:11Z")

</div>

class LedZep \< Rock::Default  
&nbsp;&nbsp;&nbsp;&nbsp;ripoff(Blues)  
&nbsp;&nbsp;&nbsp;&nbsp;include Tolkein::References::Tedious  
&nbsp;&nbsp;&nbsp;&nbsp;acts\_as\_pretentious  
&nbsp;&nbsp;&nbsp;&nbsp;has(slavering\_adulation\_of(:ignorant))  
&nbsp;&nbsp;end

&nbsp;&nbsp;Flame::on!

🙂

Regards,

Jeremy Henty

> **···**
>
> On 2006-07-14, gregarican \<greg.kujawa@gmail.com\> wrote:
> 
> > ... being a programmer and not knowing any C is like a guitarist now  
> > knowing any Led Zeppelin 🙂

---

<div class="post-metadata">

**Author:** ![gregarican](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@gregarican](https://rubytalk.org/u/gregarican)\
**Post date:** [15 July 2006 01:25 UTC](https://rubytalk.org/t/how-to-speed-up-ruby-and-make-it-as-fast-as-possible/28965/8 "2006-07-15T01:25:04Z")

</div>

Ouch. That hurt :-/ Maybe I should've said a programmer not knowing C  
would be like a southern U.S. redneck (of which I am one) not knowing  
the song "Freebird."

Jeremy Henty wrote:

> **···**
>
> > On 2006-07-14, gregarican \<greg.kujawa@gmail.com\> wrote:
> > 
> > \> ... being a programmer and not knowing any C is like a guitarist now  
> > \> knowing any Led Zeppelin 🙂
> > 
> > &nbsp;&nbsp;class LedZep \< Rock::Default  
> > &nbsp;&nbsp;&nbsp;&nbsp;ripoff(Blues)  
> > &nbsp;&nbsp;&nbsp;&nbsp;include Tolkein::References::Tedious  
> > &nbsp;&nbsp;&nbsp;&nbsp;acts\_as\_pretentious  
> > &nbsp;&nbsp;&nbsp;&nbsp;has(slavering\_adulation\_of(:ignorant))  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;Flame::on!
> > 
> > 🙂
> > 
> > Regards,
> > 
> > Jeremy Henty
