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

**URL:** <https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415>\
**Category:** Ruby\
**Created:** [July 14, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415 "2006-07-14T16:49:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/1 "2006-07-14T16:49:45Z")

</div>

-----Original Message-----  
From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] 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:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/2 "2006-07-14T17:13:58Z")

</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.

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/3 "2006-07-14T17:24:21Z")

</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:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/4 "2006-07-14T17:46:39Z")

</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 🙂

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/5 "2006-07-14T17:32:54Z")

</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

```
mov ax,@data

mov ds,ax

mov ah,62h

int 21h

mov es,bx

mov eax,0

mov ebx,081h

movzx ecx,BYTE PTR es:[80h]

```

L4: mul ten

```
movzx edx,BYTE PTR es:[ebx]

and edx,000000fh

add eax,edx

inc ebx

loop L4

```

;put code here

```
    MOV ESI,OFFSET FACT

     MOV [ESI],EAX

     DEC EAX

     MOV ECX,EAX

     CMP ECX,0

     JE PEAX

```

Fac1:

```
 MOV EBX,0

 MOV ESI,OFFSET FACT

 MOV INDEX,0

```

Fac2:

```
 MOV EAX,[ESI] ;MOV FACT INTO EAX

 MUL ECX ;MUL BY COUNT

 ADD EAX,EBX ;ADD CONTENTS OF EBX TO

```

ECX

```
 JNC Fac3 ;NO CARRY => JUMP L3

 INC EDX ;OTHERWISE INC EDX

```

Fac3:

```
 MOV EBX,EDX ;MOV CONTENT OF EDX TO EBX

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

```

POS

```
 INC INDEX

 MOV EAX,INDEX

 CMP LEN,EAX ;COMPARE LEN, INDEX

 JNE Fac5 ;NOT EQUAL => JUMP L2

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

 JZ Fac4 ;NO CARRY => JUMP L4

 INC LEN ;INC LEN

 ADD ESI,4

 MOV [ESI],EBX

 JMP Fac4

```

Fac5:

```
 ADD ESI,4

 JMP Fac2

```

Fac4:

```
 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:

```
pop eax

mov ebx,eax

mov ecx,8

cmp INDEX,0

jz I1

```

L9:

L3:

```
    mov edx,0f0000000h

and edx,ebx

push ecx

mov cl,28

    shr edx,cl

pop ecx

mov ah,02h

cmp dl,10

jb L1

add dl,37h

    jmp L2

```

L1: or dl,30h

L2: int 21h

```
shl ebx,1

shl ebx,1

shl ebx,1

shl ebx,1

loop L3

dec INDEX

cmp INDEX,0

jnz PEAX

mov ah,4ch

int 21h

```

I1:

```
inc INDEX

jmp L9

```

main endp

end main

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/6 "2006-07-14T19:54:34Z")

</div>

Jamal M. wrote:

> 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]

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

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 15, 2006, 3:28am UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/7 "2006-07-15T03:28:14Z")

</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.”

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [July 14, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/re-how-to-speed-up-ruby-and-make-it-as-fast-as-possible/98415/8 "2006-07-14T23:41:00Z")

</div>

On 2006-07-14, gregarican [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> … being a programmer and not knowing any C is like a guitarist now  
> knowing any Led Zeppelin 🙂

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

Flame::on!

🙂

Regards,

Jeremy H.
