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



cibien
Novice

Sep 19, 2012, 2:20 AM


Views: 11224
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

Andrea
Attachments: perl code.txt (52.6 KB)


FishMonger
Veteran / Moderator

Sep 19, 2012, 7:10 AM


Views: 11217
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.

Code
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.
http://search.cpan.org/~shancock/Perl-Tidy-20120714/lib/Perl/Tidy.pm

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.
http://search.cpan.org/~timb/Devel-NYTProf-4.09/lib/Devel/NYTProf.pm

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


cibien
Novice

Oct 9, 2012, 7:53 AM


Views: 9814
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.

thankyou

Andrea