Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

where does the time go?

by badaiaqrandista (Pilgrim)
on Oct 31, 2005 at 23:24 UTC ( [id://504443]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,

I have a weird problem when trying to profile my code performance. This is the oversimplified code structure:

#!/usr/bin/perl use warnings; use strict; use Devel::Timer; my $target = 'a' x 100; search (); sub search { my $t = Devel::Timer->new; $t->mark('a'); validate_search_index (); $t->mark('b'); my @first_results = another_search (); $t->mark('c'); my @results; if (@first_results) { $target =~ /y/ for (1..10_000); my @second_results = ('a') x 100; foreach my $i (@first_results) { foreach my $j (@second_results) { push @results, [ $i, $j ]; } } } $t->mark('d'); $t->report; return @results; } sub another_search { $target =~ /y/ for (1..10_000); return (1) x 100; } sub validate_search_index { my $t = Devel::Timer->new; $t->mark(0); $target =~ /y/ for (0..10000); $t->mark(1); my @the_list = list_of_something (); $t->mark(2); my @other_list; if (@the_list) { $target =~ /y/ for (0..10000); @other_list = (1) x 100; } $t->mark(3); foreach (@the_list) { $target =~ /y/ for (0..10000); } $t->mark(4); $t->report; } sub list_of_something { $target =~ /y/ for (1..10_000); my @results; foreach (1..100) { $target =~ /y/ for (1..10_000); push @results, $_; $target =~ /y/ for (1..10_000); push @results, $_; } return @results; }

The original report for search is:

Devel::Timer Report -- Total time: 1.3955 secs Interval Time Percent ---------------------------------------------- 01 -> 02 1.3502 96.75% a -> b 02 -> 03 0.0452 3.24% b -> c 03 -> 04 0.0001 0.00% c -> d 00 -> 01 0.0000 0.00% INIT -> a

The original report for validate_search_index is:

Devel::Timer Report -- Total time: 0.0042 secs Interval Time Percent ---------------------------------------------- 02 -> 03 0.0026 60.89% 1 -> 2 03 -> 04 0.0011 27.18% 2 -> 3 04 -> 05 0.0002 5.23% 3 -> 4 01 -> 02 0.0002 5.13% 0 -> 1 00 -> 01 0.0001 1.57% INIT -> 0

My question is: why does the report for search shows 1.3502 seconds for 'a -> b' (time taken to execute validate_search_index) while the report for validate_search_index shows the total time of 0.0042 seconds?

Why is there a huge difference between the validate_search_index's report and the search's?

Update: Apparently this is my fault. I just found I call a time consuming method to supply an argument to validate_search_index.

Thanks

Replies are listed 'Best First'.
Re: where does the time go?
by GrandFather (Saint) on Nov 01, 2005 at 00:11 UTC

    That's a little hard to reproduce. But if you can change this code to show the same behaviour then we may be able to help a little more.

    use warnings; use strict; use Devel::Timer; search (); sub search { my $class = shift; my $t = Devel::Timer->new; $t->mark('a'); validate_search_index (); $t->mark('b'); my $target = 'some text to search some text to search some text to + search some text to search '; $target =~ /y/ for (0..10000); $t->mark('c'); $target =~ /y/ for (0..10000); $t->mark('d'); $target =~ /y/ for (0..10000); $t->report; } sub validate_search_index { my $t = Devel::Timer->new; $t->mark(0); my $target = 'some text to search some text to search some text to + search some text to search '; $target =~ /y/ for (0..10000); $t->mark(1); $target =~ /y/ for (0..10000); $t->mark(2); $target =~ /y/ for (0..10000); $t->mark(3); $target =~ /y/ for (0..10000); $t->mark(4); $target =~ /y/ for (0..10000); $t->report; }

    Reports:

    Devel::Timer Report -- Total time: 0.0153 secs Interval Time Percent ---------------------------------------------- 03 -> 04 0.0045 29.47% 2 -> 3 04 -> 05 0.0036 23.84% 3 -> 4 01 -> 02 0.0036 23.59% 0 -> 1 02 -> 03 0.0035 22.95% 1 -> 2 00 -> 01 0.0000 0.14% INIT -> 0 Devel::Timer Report -- Total time: 0.0268 secs Interval Time Percent ---------------------------------------------- 01 -> 02 0.0195 72.63% a -> b 02 -> 03 0.0037 13.87% b -> c 03 -> 04 0.0036 13.37% c -> d 00 -> 01 0.0000 0.14% INIT -> a

    Perl is Huffman encoded by design.

      I am having a hard time to simplify the actual code because it calls other classes and creates SQL queries. What do you suggest me to do? Should I just post the original code? But it won't be runnable.

      Thanks

        Try to retain the same structure, but replace SQL and such non-portable stuff with some sort of time wasting code (like the regexs that I used) to use similar amounts of time.

        My guess is that the structure is important in some fashion (a hole in the timing perhaps), but that the details of how the time is used should not be important.


        Perl is Huffman encoded by design.
Re: where does the time go?
by bmann (Priest) on Nov 01, 2005 at 00:21 UTC
    How long does it take after $t->mark(4)?

    Never mind...

Log In?
Username:
Password:

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

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

    No recent polls found