Optimizing Ruby Scripting Efficiency examples sharing _ruby topics

Source: Internet
Author: User

The PROFILE.RB is a profiler for Ruby programs that can count and output the elapsed time of each method in order to find performance bottlenecks in program execution. This time use it to dissect the running time of the script. Using the method is simple, plus the command-line option-R profile can:

Ruby-r Profile RENAME.RB
At the end of the run, statistics are exported to the standard error output. As shown in the following illustration:

Profile statistics are the operating time of each method, divided into 2 categories. The 1th calculation is the time from the method call to the method return, called the overall time, and the 2nd is the time taken from the whole time after the time spent calling other methods in the method, called the actual time. The fields in each row of the output information have the following meanings (left to right):

The percentage of the total time that the method is executing is more or less the higher the percentage, which means that this line of code may need to be optimized

The sum of the whole time

The sum of the actual time

Number of times called

Average actual time of each call (MS)

Average overall time of each call (MS)

Method name

As you can see from the illustration above, the time spent on the execution of the script is mostly on the loop. There are two solutions: eliminating loops or reducing the number of loops. The former is difficult to achieve, for the time being has not thought of the method, perhaps there is no possibility at all. The size of the mapping in the script is 2685, so it takes 2,685 cycles to modify each filename, and the encode and gsub! in the loop are time-consuming operations. Usually the length of a file name is no more than 30 characters, and by iterating through each character in the file name, the number of loops can be reduced to no more than 30 times.

After modifying the code and then performing the Analysis command again, the result is that the script runs from 379,395 seconds to 2,418 seconds, and the performance increases by 156 times times to 2 orders of magnitude.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.