11.2.2 Measuring CPU usage The same psor top commands will also tell you how much cpu and elapsed time a process has used, of course. You can also use the Unix time command to tell you the same thing: $ time myprog 0.050u 0.180s 0:03.68 6.2% 0+0k 0+0io 98pf+0w This tells you that myprog took 3.68 seconds of realtime to run, but spent only 0.05 + 0.18 = 0.23 seconds using the CPU (it must have been doing a lot of I/O). But what if you want to find the fastest strategy among several choices? You could run each one separately and time them, of course. Fortunately, Perl can do it for you with the Benchmarkmodule, by Jarkko Hietaniemi and Tim Bunce. Let’s say that you’re initializing a large hash inside a loop that gets executed many times and you want to know whether it’s faster to use a hash slice or a foreach loop: use Benchmark; timethese (400, { undef => ‘@h{1..10000} = ()’, set => ‘@h{1..10000} = (1)x10000′, loop => ‘$h{$_} = undef for 1..10000′, loopset => ‘$h{$_} = 1 for 1..10000′, });
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check Actions servlet hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
Dr. Leonard McCoy in Star Trek: The Devil in the Dark Computer scientists talk knowledgeably about “Turing-complete” programming languages, by which they mean a language that can be shown to be equivalent to a Turing machine and therefore provably able to solve any computation that can be expressed as an algorithm. A Turing machine is a hypothetical device invented by Alan Turing, and consists of a long tape passing under a read/write head that is capable of reading and printing characters on the tape according to a table of rules specifying what to do. Unfortunately, no implementation of any programming language does or ever could live up to the strict definition of “Turing-complete,” since Turing specified that his machine was equipped with an infinitely long tape. He also specified that the machine was allowed an arbitrary amount of time to solve a problem, which is a luxury that few people outside of academia can afford. This chapter is about what to do when your finite resources are too finite. We’ll discuss how to react when you run out of time, memory, or disk space. 11.1 Optimize for People First, Resources Later Note that we didn’t say, ” how to avoid running out of .” There’s a good reason for this: If you worry about it from the beginning, you’re optimizing for the wrong thing. While you’ll see a lot of carping from seasoned professionals on Usenet arguing that such-and-such code isn’t as efficient as their latest mutation of it, understand that they’re coming from a place where the mind-bending code they’ve just written is as clear to them as anything else. More often than not, they’re coming up with these performance enhancements to make the problem more interesting for themselves. They would not disagree with the advice we’re about to give, however:
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
Optimize your code for readability and maintainability first; make performance optimizations only after profiling of a working version demands it. By far the most dominant resource cost in software today is not CPU speed, RAM, or disk space; it’s people. The time spent by people developing, documenting, maintaining, inheriting, reading, porting, modifying, enhancing, understanding, or debugging an application is the most precious and scarce resource in information technology. Therefore our advice up to this point has focused on how to develop applications to make the best use of that time. Also, don’t underestimate the ability of Moore’s Law to help you. In other words, if you’re developing an application that will not be operational for another year and a half, by that time processors will be twice as fast, RAM will be half the price, and disk drives will be twice as big. If you’re able to trade developer time for procurement dollars (and it’s strange how many companies devalue their employees’ time in comparison to their hardware even though their bank account doesn’t know the difference) and the problem can be solved more cheaply by better hardware, get it. There will be plenty of other opportunities to practice writing efficient programs because sometimes you really won’t have enough resources to run them, and then you will have to make them do better. Whatever you are developing, though, don’t assume you know in advance what resource you may run out of. The aforementioned seasoned professionals know that the ability of even the best of them to predict such things is notoriously unreliable.[1] [1] This is not to say that there aren’t occasions when resource limitations are fairly obvious. If you’re archiving the entire World Wide Web, you should probably be concerned about disk space before you start. (If you are archiving the entire World Wide Web using Perl, please drop us a line; we have a few questions to ask you.) We will show how you can optimize your programs for speed, memory, or disk space. However, these are not independent variables; frequently there are trade-offs (e.g., gaining memory at the expense of disk space). Equally common are parallel gains: For instance, as you improve your code style, memory usage may go down and execution speed may go up due to fewer memory accesses. 11.2 Benchmark It! Because it’s generally so hard to tell by looking at the code where the bottlenecks are (especially since they may move depending on your hardware configuration), you have to be able to measure them objectively. You may have no recourse but to use the native tools of your operating system to measure virtual memory usage in particular. (See Perl of Wisdom #29.) In this respect we confess we are far more experienced on the various Unix platforms than on the Macintosh or Windows.[2] [2] We tried measuring process size on Windows 98 using a freeware tool called prcview and were baffled by the results. It appeared that every process out of a couple of dozen was using at least 20 MB. 11.2.1 Measuring Memory Usage The top program available on most Unix variants is very good for determining how much processor time and virtual memory are being used by current processes. To gather the same
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
sub get_proto { return $protocol; } sub set_proto { $protocol = shift; } } # End of block containing closures print get_proto(); (We’ve removed anything not necessary to understanding this point.) If you run this, you’ll see that it indeed knows what the default protocol is, and you’ve successfully isolated that shared variable to the two subroutines that use it. Let’s say that later you decide that the subroutines are better off declared at the end of the file so that you can start reading with your main program Perl can handle forward references, right? So you change it to print get_proto(); { my $protocol = “ftp”; # default sub get_proto { return $protocol; } sub set_proto { $protocol = shift; } } # End of block containing closures But now when you run the program, get_proto will complain about an uninitialized variable. The reason is closely related to our previous error: when we call get_protoin the first line, $protocol hasn’t been initialized because we haven’t gotten to the line where that happens yet. The reason there’s no complaint from strict about it being an undeclared variable in the closures is that it has been declared; however, that happened at compile time, and the assignment of the default value won’t happen until run time. You can avoid this problem naturally by following Perl of Wisdom #18. If you don’t yet understand closures but you’re intrigued, read “What’s a closure?” in the core Perl documentation section perlfaq7. Chapter 11. Resource Failure “He’s dead, Jim.”
Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
You have a subroutine that should set a private variable based on some condition. Not unreasonably, you might code something following this general form: sub ordinal { my $what = shift; my $res = ‘first’ if $what == 1; $res = ’second’ if $what == 2; return $res; } But you’d be wrong. If we run this through perl-lwith for(1..3){ print ordinal($_)}, we get the result: first second second Eh? Shouldn’t that last line be a warning about an undefined value? Regrettably, you’ve fallen afoul of an internal optimization in Perl[4] that keeps the storage assigned to lexical variables around after they’re gone, on the premise that they may be needed again shortly. The my statement has both a compile-time effect (declaring the variable) and a run-time effect (setting it to undef, unless the statement is qualified by a failed condition). [4] That may change in a future version. Never qualify a my statement with a condition. Note that this is not talking about putting my statements inside conditions: if ((my $tag = $element->tag) ne ‘p’) { # Code using $tag } while (my ($key, $value) = each %hash) { … } which is a very good idiom in that it concisely limits the scope of the new variable to precisely the block in which you need it. 10.6 Bringing Some Closure Understanding how lexical variables work is worth the effort. Suppose you have advanced to the stage of understanding closures give yourself a pat on the back and you decide to code a few subroutines that use a private variable that way: { my $protocol = “ftp”; # default
Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
my %port = (http => 80, https => 443, ftp => 79, nntp => 119, gopher => 70); while () { if (my ($url) = m#((w+)://S+[w])#) { $url = ~ s#:$port{$2}/#/# if $port{$2}; print “URL found: $urln”; } } (Yes, there are better ways of isolating URLs from text; this code doesn’t even find more than one per line. But that’s not the point we’re about to make.) Flushed with the cleverness of our code, we feed it the input: The President (http://www.whitehouse.gov:80/president/) said today that he liked ferrets (http://www.ferretcentral.org/), and was considering placing a picture of one (ftp://ftp.optics. rochester.edu:79/pub/pgreene/icons/central-logo-t.gif) on the next version of the national flag; but he thought they were too often confused with gophers (gopher://gopher.tc.umn.edu:70/11/). expecting to see the output:[3] [3] We indented continuation lines in the input and output to make them easier to read. URL found: http://www.whitehouse.gov/president/ URL found: http://www.ferretcentral.org URL found: ftp://ftp.optics.rochester.edu/pub/pgreene/ icons/central-logo-t.gif URL found: gopher://gopher.tc.umn.edu/11 But instead, we see: URL found: 4294967294 URL found: 4294967295 URL found: 4294967294 URL found: 4294967294 If you run this program through Deparse, you’ll see the line $url = ~s[:$port{$2}/][/] if $port{$2}; which gives us just enough of a clue if we haven’t found the typo already. The space we accidentally put inside the regex binding operator has changed its meaning to “$urlis
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
assigned the one’s complement of the result of this substitution on $_ (which in a scalar context is the number of substitutions made).” 10.3.1 Flashback Remember in Chapter 8 we wondered why Perl placed the error with the transposed for clauses on the wrong line? Now we know how to find out! $ perl -MO=Deparse temp.pl Useless use of numeric lt (<) in void context at temp.pl line 13. my $fmt = '%10s ' x 5; printf "$fmtn", qw(Kelvin Celsius Rankine Fahrenheit Reaumur); $fmt = '%10.2f ' x 5; my $kelvin = 0; while ($kelvin += 10) { my $celsius = $kelvin - 273.15; my $rankine = $kelvin * 9 / 5; my $fahrenheit = $rankine - 459.67; my $reaumur = $celsius * 4 / 5; printf "$fmtn", $kelvin, $celsius, $rankine, $fahrenheit, $reaumur; } continue { $kelvin < 500 } Well, look at that. The dirty truth about for loops emerges: they're nothing but while loops in fancy clothes. The test clause got relocated to a continue block, where, of course, it makes no sense, being an expression in void context. 10.4 printf Formats Don't Impose Context If you're a die-hard C programmer (and is there any other kind?), you might think that to print the number of entries in an array, since the %dprintfformat specifier means "number," well gosh, a number is a scalar, and so that's the context it'll put its argument into: printf "Number of entries in @foo = %d", @foo; but by now you know us too well to fall for that yes, @foo is in list context here, because that's what printf is prototyped to take. So you'll either get the first entry of @fooprinted, or a complaint if that's not a number. 10.5 Conditional my
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
The specification of that function says that if $filedoesn’t exist, stat returns undef, which certainly isn’t an object. Therefore $filedoesn’t exist. And the reason it doesn’t exist is because $file doesn’t contain an absolute path and therefore is interpreted relative to the current directory. The current directory won’t contain files with the same names as the ones we read with readdir except by coincidence and except for the two files that every directory contains (. and ..). This is why we get some initial output that makes it look as though the script is working. It isn’t; it’s reporting the times for different . and .. from the ones we wanted. The fix is easy: just qualify $filewith $dir: use strict; use File::stat; my $dir = shift || ‘.’; opendir DIR, $dir or die “Error opening directory $dir: $!n”; while (defined (my $file = readdir DIR)) { print “File $file was last modified on ” . localtime(stat(”$dir/$file”)->mtime), “n”; } closedir DIR; [1] This works even if $diris relative or contains ../. [1] Using / as a directory separator works on Unix and Windows (Perl turns it into a on the latter). A truly operating system-independent way of forming the path would use File::Specand replace “$dir/$file” with File::Spec->catfile($dir, $file); 10.3 But What Did It Mean? The following program is designed to reprint its input lines prefixed with an increasing date that skips weekends: #!/usr/bin/perl -w use strict; use Time::Local; my $t = timelocal(0,0,0,31,11,99); # Dec 31 1999 while (<>) { my ($m, $d, $y) = (localtime $t)[4,3,5]; $y %= 100; $m++; print “$m/$d/$y $_”; dp { $t += 86400 } while (localtime $t)[6] == 0 || (localtime $t)[6] == 6; } However, the output shows a date that doesn’t change:
Hint: This post is supported by Gama besplatan domen provider
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
12/31/99 This is the first line 12/31/99 This is the second line 12/31/99 This is the third line Running under the debugger won’t reveal anything other than the fact that $t isn’t being increased in the do block. Wait a minute. It doesn’t say do, does it? It’s a typo: dp. Yes, we could fix the program right now. But curiosity demands that we wonder why Perl has no objections! What could it be thinking we mean? Enter the B::Deparsemodule. The B stands for “back-end,” which is to say, this module accepts input from the Perl compiler after it has created a binary opcode tree from a program. The whole purpose in life of B::Deparse is to turn that opcode tree back into Perl. For instance, you can use it to turn a subroutine reference into program text in the middle of a program. Deparseisn’t quite perfect you may see some odd output, at times even incorrect output but it’s getting there.[2] Let’s see what we get when we run it on just a part of the line of code that’s bugging us. Notice that we don’t invoke it with a use statement but rather with a funny variant of the -Moption designed for back-end modules: [2] Larry Wall said that Perl 5 should be translatable into Perl 6 via a kind of Deparse module, so we can expect considerable progress. % perl -MO=Deparse -e ‘dp { $t += 86400 } while (localtime $t)[6] == 0′ do { $t += 86400 }->dp while (localtime $t)[6] == 0; -e syntax OK Aha! Perl is interpreting our typo as a use of the indirect object method syntax it thinks we want to call the dpmethod of whatever the block $t += 86400 evaluates to. So this is no longer a doBLOCKwhileEXPRESSION construct (which guarantees the block will be executed at least once). Instead it is a STATEMENTwhileEXPRESSIONconstruct (where the statement is do { $t += 86400 }->dp), which means that the statement will be executed only if the expression is true, which in this case, it isn’t to begin with. The Deparsemodule can often explain how perl is parsing your code. Note: Deparse only came into the core with version 5.005, and it’s documentation has some additional options worth checking out, like the ability to totally parenthesize expressions so you can see how precedence is determined. (And if you see ‘???’in the output, that stands for a constant in the input that was optimized by perl to the point where its value was not available to Deparse.) Here’s another example in which Deparse helps. Let’s say you’re scanning text to pull out URLs and want to clean them up a little by removing redundant default port numbers:
Hint: This post is supported by Gama besplatan domen provider
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •
An even better choice would be the oroperator introduced in Perl 5, which has such low precedence that you can leave out the parentheses on open: open FH, $file or die “Error opening $file: $!n”; 10.2 Reading Directories This has bitten us more times than we care to admit: readdir() returns the list of filenames in the directory, but they are not qualified by the directory itself. Consider the following program: use strict; use File::stat; my $dir = shift || ‘.’; opendir DIR, $dir or die “Error opening directory $dir: $!n”; while (defined (my $file = readdir DIR)) { print “File $file was last modified on ” . localtime(stat($file)->mtime), “n”; } closedir DIR; It takes a directory as its argument, defaulting to the current directory. This program prints out the names and modification times of all the files in the current directory. If run with no arguments, the program runs fine. If we then run it with an argument (this example is on Unix), it appears to start working and then fails: File . was last modified on Sun Mar 28 14:36:27 1999 File .. was last modified on Tue Aug 3 15:29:12 1999 Can’t call method “mtime” without a package or object reference at mod_times.pl line 9. What happened here? First, we see the error refers to line 9 and that perl attempted to invoke the mtimemethod on something that wasn’t even an object. What was the object? It was the result of the call to stat($file) (we’re using the stat routine from the File::stat module here, instead of the core perl stat routine that returns a list; it saves us from having to look up the index of the mtimeelement).
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services
Posted by mario at 10:32 AM. Filed under: perl
No Comments • Trackback • Permalink •