almut, thanks for the idea. I have to admit, I am very wary of closing STDOUT, even if the job is running in cron.
In keeping with my offer, I tried a solution using Net::Ping, which, indeed, looks like exactly what I am looking for, as talexb mentioned.
Here are some preliminary results. I used
I put the return value checks in the timed segments to make the comparison as "fair" as possible. Also note that the program is a loop over 18 distinct hosts, not the same host repeatedly.#some stuff like use strict, etc. Readonly my $udp_pinger => Net::Ping->new('udp',1); #other stuff, then beginning of loop my $t0 = [ gettimeofday() ]; `ping -q -w 1 -c 1 $host`; die if ($?); my $e0 = tv_interval($t0); print "Ping with backticks took $e0 seconds"; $t0 = [ gettimeofday() ]; $udp_pinger->ping($host) || die "udp ping failed for $host"; $e0 = tv_interval($t0); print "Ping with Net::Ping took $e0 seconds"; print ".."; #end of the loop
Results:
Ping with backticks took 0.004804 seconds Ping with Net::Ping took 0.002682 seconds .. Ping with backticks took 0.005873 seconds Ping with Net::Ping took 0.001528 seconds .. Ping with backticks took 0.005679 seconds Ping with Net::Ping took 0.001115 seconds .. Ping with backticks took 0.005465 seconds Ping with Net::Ping took 0.001108 seconds .. Ping with backticks took 0.005369 seconds Ping with Net::Ping took 0.001098 seconds .. Ping with backticks took 0.005465 seconds Ping with Net::Ping took 0.001521 seconds .. Ping with backticks took 0.006113 seconds Ping with Net::Ping took 0.001071 seconds .. Ping with backticks took 0.005269 seconds Ping with Net::Ping took 0.001227 seconds .. Ping with backticks took 0.005122 seconds Ping with Net::Ping took 0.001262 seconds .. Ping with backticks took 0.005421 seconds Ping with Net::Ping took 0.00123 seconds .. Ping with backticks took 0.00513 seconds Ping with Net::Ping took 0.001106 seconds .. Ping with backticks took 0.00501 seconds Ping with Net::Ping took 0.001211 seconds .. Ping with backticks took 0.005031 seconds Ping with Net::Ping took 0.000958 seconds .. Ping with backticks took 0.005107 seconds Ping with Net::Ping took 0.001256 seconds .. Ping with backticks took 0.005139 seconds Ping with Net::Ping took 0.000981 seconds .. Ping with backticks took 0.005484 seconds Ping with Net::Ping took 0.001086 seconds .. Ping with backticks took 0.003778 seconds Ping with Net::Ping took 0.000643 seconds
Booyah! Knocks the socks off of what I had so far. I don't know what the overhead of using the module and/or instantiating the Net::Ping object are, so the comparison still isn't "fair" until you figure those and amortize them over the actual calls to ping(), but I am comfortable with the result being better than what I had. (And for more reasons than it just being faster)
Also, the data above are (IMO) a good representative sample, and one thing I have noticed is that the first call is always the slowest. I don't know what kind of optimization happens, but the second and later pings are (so far) always at least twice as fast as the first.
Finally, pretty convenient that 'backticks' and 'Net::Ping' have the same number of characters, eh?In reply to Re: I think I just found a good reason to use backticks in a void context.
by OfficeLinebacker
in thread I thought I found a good reason to use backticks in a void context, but I was wrong.
by OfficeLinebacker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |