Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Optimize code in Ruby?

luis

Member

Well, i like to learn by watching others scripts, and trying myself, but i want to know, how i can reduce the size of the code?, how can i optimize the code? any tips or anything that can help me!? please give examples or something i can understand better... thanks a lot!  :smile:
 

khmp

Sponsor

Optimize and Ruby in the same sentence. Since I know very little about Ruby inner workings I'll tell you the most common optimizations or things to avoid. Division operations (for floats avoid multiplication as well), oh and conditionals. The way I remember it from class was that the CPU does branch prediction and assumes its true and false and partially does the work in either direction. It discards the work that was wrong. Although it probably has some preference hopefully if the wind blows in a particular direction. I'm sure if Brewmeister, Prexus or etheon see this they'll have much more information on your request. Reducing code size however.

Conditional statements:
Code:
if test
  do
end
Is the same as:
Code:
do if test

Make use of the ternary statement.
Code:
if test
  a = 1
else
  a = 0
end
Is the same as:
Code:
a = test ? 1 : 0

Simple one line iterators:
Code:
collection.each do |item|
  do(item)
end
Is the same as:
Code:
collection.each {|item| do(item)}

Multiple assignment:
Code:
a = 0
b = 0
c = 0
Is the same as:
Code:
a = b = c = 0

Or with different values:
Code:
a = 0
b = 5
c = 'Truck'
Is the same as:
Code:
a, b, c = 0, 5, 'Truck'

Oh a neat little trick for multiplying or dividing by a power of two. Bit shifting a number by 1 is the same as a multiplication/division by two.
00000110 = 6
If I shift it right once
00000011 = 3
If I shift it left twice which is like multiplying by 4
00001100 = 12

By the way its the ">>" "<<" operators.
Code:
a = 12
a >>= 1
p a # prints 6
a <<= 2
p a # prints 24

More huh? Um... Constant values are much more appreciated than dynamic values speaking from the cpu's point of view. They're less time spent in a calculation if the code already has the numbers laid out for the cpu. For example:

Code:
toad = 'A frog but smaller'
p toad
The above code is resolved at runtime. That will first create a local variable and save a string of data to it and then prints it. When it goes to print it has to perform a variable lookup and resolve it.

Code:
TOAD = 'A frog but smaller'
p TOAD
Hopefully during compilation rather than runtime all instances of TOAD are replaced with the string of data and that saves the cpu the time of having to look it up during runtime.

I don't know if Ruby in fact does this but in C such a thing exists. After writing all that I truly wonder how much time you actually save...

Good luck with it luis! :thumb:
 

luis

Member

wow! good! that what i needed! thanks a lot! really, i like to make the code look more complicated! haha! if anyone has any other idea to help me! please post it! and also, what are others advanced optimizations i can do?
 

loam

Member

Zeriab":3csf9qdo said:
You can try reading Blizzard's Scripting in RGSS Ruby for Intermediate and Experts
I hope it will be of use to you ^^

Yo, this explains section 7.4, particularly the line that says:

wikipedia":3csf9qdo said:
"The usual operators for conditional expressions, and and or, do not follow the normal rules of precedence: and does not bind tighter than or. Ruby also has expression operators || and && which work as expected."

In the other thread I got the impression you were reviewing the document so I thought I ought to give you a heads up :tongue: I imagine the author would be interested in knowing.
 

loam

Member

etheon":3k9r7fxh said:
Eh, newer versions of Ruby using YARV, in other words, a virtual machine, will run as compiled bytecode; thus, faster.

Which means absolutely nothing for RGSS if it's still built on an old version of ruby, which it is. So unless someone patches rpg maker...? Just wondering what you mean.
 

e

Sponsor

Well, hopefully newer versions of the RPG Maker softwares will use Ruby 1.9.x or 2.x. I wouldn't be surprised to see a patch of RMVX at some point with the newer Ruby interpreter.
 

khmp

Sponsor

etheon":3ktd75mn said:
Well, hopefully newer versions of the RPG Maker softwares will use Ruby 1.9.x or 2.x. I wouldn't be surprised to see a patch of RMVX at some point with the newer Ruby interpreter.

If only. I'm sure instead a new RM will come out called "RPG Maker: The Quest for Moar Money", the acronym being RM$$. Featuring an easier interface with only one button, "Make Game", with a corresponding check box, "Final Fantasy". Sorry I was just listening to "The Escapist".
 

Zeriab

Sponsor

It's not a bug, it's a feature  :dead:

Thanks for the heads up Loam ^^
I will pass the information on to the author.

I am glad you like it and I am sure Blizzard is as well.

@khmp:
Arrays are not better to use than hashes. Arrays are in some cases better to use than hashes. In some cases hashes are better to use than arrays. In some cases the difference insignificant.
It depends on the situation. The reason on the emphasis the author uses on the bad uses of hashes is due to the history of hashes being overused in RMXP script. To make matters worse many non-scripters has gotten accustomed to hashes which reinforces the overuse of hashes.
It is mainly a case of getting a point across by simplification.
Analyze what to use in the specific situation.

*hugs*
- Zeriab
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top