Home: Perl Programming Help: Advanced:
speed up perl code


Sep 19, 2012, 2:20 AM

Views: 11334
speed up perl code

Hi, I make this perl code (convert excel files in one xml file), it works well but is too much slow, 4 minute to create the xml file (12mb). Can you help me to modify this perl code to speed up the conversion?

thanks Smile

Attachments: perl code.txt (52.6 KB)

Veteran / Moderator

Sep 19, 2012, 7:10 AM

Views: 11327
Re: [cibien] speed up perl code

Start by adding the strict and warnings pragmas and remove the -w switch. Those pragmas should be in every script you write.

use strict; 
use warnings;

Run your code thru Perl::Tidy to fix the formatting. Your improper use of indentation makes it harder to follow the flow of the code.

Use Perl's for loop style instead of the C style for loop. Perl's style is not only cleaner, it executes faster.

Your repeated calls to decode() on the same cell value is wasteful. Call it once and assign it to a var that is used when checking/testing for different values.

Use Devel::NYTProf to profile your code to see where it's spending its time and thus know what sections need to be reworked.

Refactor sections of the code into subroutines. Having the entire script in an 800 line block is a sign of poor design.


Oct 9, 2012, 7:53 AM

Views: 9924
Re: [FishMonger] speed up perl code

thanks for your advice, with Devel::NYTProf,

I understand that it speding a lot of time here:

" while( ($filename = readdir(DIR))){ "

the module Spreadsheet::ParseExcel spend a lot of time to open the excel file...

Is fast to open light .xls files, and very slow to open big (4mb) .xls files

I think is nothing to do.