NullVoxPopuli
NullVoxPopuli

Reputation: 65083

is ruby ever compiled? does using long variable names affect performance?

I just thought of something: if ruby is just a scripting language: wouldn't that mean that variables and method names that are long would run / get called slower than if they had shorter names? I think they would run slower due to string searching and manipulation.

But, I'm no computer scientist, so.. this is too low level for me.

is there a way to run a rails app with compiled code?

Upvotes: 1

Views: 583

Answers (3)

Paul L
Paul L

Reputation: 2260

My answer is that Ruby will be compiled on fly, and long variable name wouldn't affect performance in any meaningful way.

What end up executed is not text you write as Ruby code, it is machine code. So I would image it goes like this:

Ruby code call Ruby library written in C, and the C functions will be compiled code. What is running is Ruby code translated to machine code in memory, it is just not saved as compiled code on disk, so each time the translation would be done on the fly.

I think at this point it is clear that variable name wouldn't affect performance as you expected, but I image it could have affect performance somehow, but not something you need worry about.

The best way would be benchmark it.

Upvotes: 0

Chuck
Chuck

Reputation: 237010

Just about every version of Ruby — MRI, JRuby, Rubinius, MacRuby, etc. — uses some sort of compiler, though not generally an AOT compiler. Variable names don't make much of a difference beyond the file size, at any rate. If you're really worried about this, Ruby is probably the wrong language for you, because I can pretty much guarantee your Ruby code and associated setup will be way slower than the Ruby compiler itself. (And incidentally, even compilers often keep variable names around, even for languages like C. Otherwise different files couldn't see each other's variables and functions. But walking through variable names doesn't comprise a very big portion of any program's runtime, in Ruby or pretty much any other language.)

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230276

You should not worry about variable name lengths.

You should be worrying about writing readable code instead. Iron is cheap nowadays, brains are not :-)

And yes, sometimes you can compile Ruby.

Rubinius allows compilation to bytecode. MacRuby compiles to native code.

Upvotes: 5

Related Questions