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.