Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Increase speed of nested loop Win32 Excel Manipulation

by Sasuke300 (Initiate)
on Feb 16, 2019 at 17:55 UTC ( [id://1229997]=perlquestion: print w/replies, xml ) Need Help??

Sasuke300 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys I have a problem with the speed of one part of my perl script. My script deals with a manipulation of a excel file. The part where the speed is very low is a nested loop and I have no idea how to avoid that. I can show you the part:

use strict; use warnings; use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const "Microsoft Excel" for ($begincol;$begincol<$totcol;$begincol++) { for ($beginrow;$beginrow<$maxrows;$beginrow++) { my $cap = $Sheet2->Cells($beginrow,5)->{'Text'}; my $app = $sheet2->Cells(3,$begincol)->{'Text'}; my $yes = "yes"; if (index($app, $cap) != -1) { $Sheet->Cells($beginrow,$begincol)->{Value} = $asis; } } $beginrow = 7; }

Replies are listed 'Best First'.
Re: Increase speed of nested loop Win32 Excel Manipulation
by Perlbotics (Archbishop) on Feb 16, 2019 at 18:51 UTC

    You could shift those parts from the current loop towards the outer loop that do not depend on the variables of the current loop (= are constant in that loop). This avoids unnecessary re-computation and might slightly improve performance.

    use warnings; use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const "Microsoft Excel" my $yes = "yes"; #-- moved: constant for both loops for ($begincol;$begincol<$totcol;$begincol++) { my $app = $sheet2->Cells(3,$begincol)->{'Text'}; #-- moved: consta +nt for next loop for ($beginrow;$beginrow<$maxrows;$beginrow++) { my $cap = $Sheet2->Cells($beginrow,5)->{'Text'}; #-- moved out of inner loop: my $app = $sheet2->Cells(3,$begi +ncol)->{'Text'}; #-- moved out of both loops: my $yes = "yes"; if (index($app, $cap) != -1) { $Sheet->Cells($beginrow,$begincol)->{Value} = $asis; } } $beginrow = 7; }

Re: Increase speed of nested loop Win32 Excel Manipulation
by poj (Abbot) on Feb 17, 2019 at 07:59 UTC

    As swl suggested, you can read/write data as a range.

    #!perl use strict; use Win32::OLE; use Win32::OLE::Const "Microsoft Excel"; use Data::Dump 'pp'; $Win32::OLE::Warn = 3; my $Excel =Win32::OLE->new('Excel.Application'); $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Open('c:/temp/Book1.xlsx'); my $Sheet = $Book->Worksheets("Sheet1"); my $begincol = 2; my $beginrow = 6; # B6 my $endcol = 4; my $endrow = 10;# D10 my $c1 = $Sheet->Cells($beginrow,$begincol); my $c2 = $Sheet->Cells($endrow,$endcol); my $data = $Sheet->Range($c1,$c2)->Value; pp $data; $Sheet->Range("A16:C20")->{'Value'} = $data; $Excel->Quit(); undef $Excel;
    poj

      ++. Even in VBA, this sort of IO is very slow & the docs recommend this approach.

      Regards,

      John Davies

Re: Increase speed of nested loop Win32 Excel Manipulation
by swl (Parson) on Feb 16, 2019 at 22:06 UTC

    If you have not done so already, I would suggest you use Devel::NYTProf to identify where all the slow points are in your code. It will save a lot of guesswork, and there might be other parts of your codebase you had not thought of as causing bottlenecks.

    In terms of the example code you gave, I have not used Win32::OLE, but I/O can often be a bottleneck in code. Is it possible to avoid repeated reading and writing of the file? i.e., read as much as you can in batch mode (a data range), update it in perl, and then write it back in one go? That said, you should run your code through Devel::NYTProf to make sure this is the case before you do any editing, and keep track of the timing changes to see if it makes a meaningful difference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1229997]
Approved by Perlbotics
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found