Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

SLOW DOWN, you move too fast.....

by perlNinny (Beadle)
on Aug 08, 2006 at 14:32 UTC ( #566170=perlquestion: print w/replies, xml ) Need Help??

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

I have a script which is to print 2400 lines but it seems to stop midway through line 2327. Am I printing too fast? How do I slow it down? Code:
my @keys_output = keys(%output); my $temp = @keys_output; print "$temp in output\n"; foreach my $data (@keys_output) {print $data.",".$output{$data}."\n";} #prints 2327 lines.

Considered by Albannach: perhaps a more meaningful title like 'print stops too soon'
Unconsidered by planetscape: keep votes trump edit votes ( keep:6 edit:37 reap:0 )

Replies are listed 'Best First'.
Re: SLOW DOWN, you move too fast.....
by Limbic~Region (Chancellor) on Aug 08, 2006 at 14:43 UTC
    First, your code would be better written as:
    my $num_keys = keys %output; print "$num_keys in output\n"; for my $key (keys %output) { print "$key,$output{$key}\n"; }
    I would ask how you are measuring the number of lines printed. The speed with which it is printed shouldn't have anything to do with the total amount of output though buffering may be an issue depending on how you are counting. More information is needed.

    Cheers - L~R

Re: SLOW DOWN, you move too fast.....
by ptum (Priest) on Aug 08, 2006 at 14:38 UTC

    I don't see any obvious reason why only 2327 lines would be printed out of 2400. Is it possible you don't really have 2400 distinct key entries in your hash?

    As you may be aware, hashes handle key collision by overwriting the existing hash entry, so that if I had two lines with key 2104, the second value to be added to the hash under that key would overwrite the first value.

    I don't think you have given us enough to identify the problem. If you really just want to slow things down, add a sleep inside your foreach.

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: SLOW DOWN, you move too fast.....
by ikegami (Patriarch) on Aug 08, 2006 at 14:40 UTC

      ... except that, as far as any of us can tell from the code, it prints to standard output (line buffered by default) and includes a newline. Even if all of the variables in that line are empty, this will still print at least 4654 characters, which is larger than most default standard output buffers I've seen in use (and the user is seeing at least some output).

      Unless there's an IO layer somewhere with a very weird buffering behavior, I see no possible way this could have anything to do with buffering. print calls to STDOUT attached to a terminal and including a newline almost never have buffering problems.

        First, you have no way of knowing how many bytes were printed, so you have no idea if the number of bytes is a multiple of a reasonable buffer size. How then can you claim the buffering is weird?

        Second, the number of bytes that didn't print is even more telling. Could 73 lines fit in a normal buffer? Probably.

        Thirdly, why do you assume he's printing to a tty? To count the lines exactly, he probably redirected stdout. That disables line buffering.

        Finally, that a line was only partially printed is a very good indicator of a buffering problem.

Re: SLOW DOWN, you move too fast.....
by liverpole (Monsignor) on Aug 08, 2006 at 16:42 UTC
    Hi perlNinny,

    I agree with imp that there may likely be some strange data in that line.

    Here's a subroutine you could use to look at the line in question:

    # # decode_string: display the ascii value of each character of a stri +ng # # In: $1 ... the string the decode # $2 ... (optional) number of characters to show per line # $3 ... (optional) a flag: nonzero = hilight non-printing c +hars # $4 ... (optional) a flag: 0 = show hex, nonzero = show dec +imal # $5 ... (optional) the symbol representing non-printing char +s # # Result: Displays the value of each ascii character in the string, # with a summary of the string length, including number of # printable and non-printable characters. # sub decode_string { my ($str, $nper, $b_hi, $b_dec, $nonasc) = @_; # Defaults $b_dec ||= 0; $nper ||= ($b_dec? 12: 16); $nonasc ||= '.'; $b_hi ||= 0; # Anonoymous subroutines my $pprintable = sub { ($_[0] < 32 || $_[0] > 126)? 0: 1 }; my $pshow_asc = sub { my ($idx, $pchars, $width) = @_; return unless @$pchars; $idx and map { print " " x (3 + $width) } ($idx .. $nper-1); print " ["; map { printf "%s", $pprintable->(ord $_)? $_: $nonasc } @$pcha +rs; print "]"; @$pchars = ( ); }; # Variable initialization my ($pasc, $idx, $ntext, $nnontext) = ([ ], 0, 0, 0); my $hilight = $b_hi? "\e[101m": ""; # Display the string print "-" x 79; my $len = length($str); foreach my $i (0 .. $len-1) { ($i % $nper) or $pshow_asc->($i, $pasc, $b_dec); ($i % $nper) or printf "\n %7d|", $i; push @$pasc, my $char = substr($str, $i, 1); my $hlchar = ""; if ($pprintable->(ord $char)) { ++$ntext; } else { ++$nnontext; $hlchar = $hilight; } printf $b_dec? " $hlchar%3d\e[m": " $hlchar%02x\e[m", ord $cha +r; } # Summary $pshow_asc->($len % $nper, $pasc, $b_dec); print "\n\n String length = $len "; print "($ntext printable, $nnontext non-printable)\n", "-" x 79, " +\n"; }

    Try calling decode_string($string, 0, 1), where $string is the line you want to look at, and it should hilight (in red) any bytes in the line which are non-printable.

        Sleeping even 1 second will take 40 minutes to run through the 2400 items.

    It's a good point.  But you could slow things less abruptly with something like:

    select(undef, undef, undef, 0.05);

Re: SLOW DOWN, you move too fast.....
by rhesa (Vicar) on Aug 08, 2006 at 14:41 UTC
    you can always pipe through more:
    perl | more
    That way you get paged output.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-08-19 05:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found