开发者

Running time assumptions in Ruby

开发者 https://www.devze.com 2022-12-31 19:18 出处:网络
Ruby looks a very cool language. I\'ve started learning it for the past two three days. One thing that appeals me in Ruby is its simplicity. Very clean code is possible. However, the internal implemen

Ruby looks a very cool language. I've started learning it for the past two three days. One thing that appeals me in Ruby is its simplicity. Very clean code is possible. However, the internal implementations of Ruby is not exposed to the outside world. I learnt that Ruby is written in various languages depending on the flavors.

The one thing that bugs me (since I come from a Java background) is the memory usage and speed due to its stronger level of Abstraction. I just want to hear what Ruby users have to say regarding this. By memory usage and speed, I mean in Java i will decide when to go for an ArrayList, when to go for a LinkedList, when to go for a Hash. I know their implementations completely, even if I dont, I can have a peek into them. But in Ruby, its not straightforward (I know it should be an efficient implementation). But, how will I be sure that I am not over-using t开发者_运维知识库hings. For example what happens when I simply define an array. Is it going to be a typical bunch of consecutive memory locations or different implementation altogether?

Plus, Ruby written in various languages follows the similar algorithms implemented or it differs for languages.

Just curious...


Ruby's memory usage is definitely a pain point when it comes to deploying. Take for example a well-developed (in both meanings of the term: 1) developed using best practices and 2) extensive) Rails application would "gladly" take up more than 256mb on the system where it's deployed. But you get all that pretty useful syntactic sugar with it. That's the "good" to the "evil".

I wouldn't be too concerned with how Ruby stores an Array object, as was your example. Even as an experienced Ruby developer, I don't care how it's stored because I am assuming that the language creators have known how to optimize the language and have done that well. But, every release is faster.

There's two places that come to mind for where you should be careful about memory usage in your application:

  • Reading large files
  • Creating lots of objects

If you're going to be reading a 160MB file into your Ruby program, that's 160MB+ of memory that's going to be taken.

Then if you create too many objects, each object has its own memory allocation. Let's say it's 16kb per object. If you create a million objects, that's 16,000,000kb, or 16GB.

Just be careful!

You may also be interested in this handy tool.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号