[asterisk-users] Benchmarking AGI performance in C, PHP, and Perl
Tzafrir Cohen
tzafrir.cohen at xorcom.com
Tue Jul 12 19:33:37 CDT 2011
On Mon, Jul 11, 2011 at 06:45:08PM -0700, Steve Edwards wrote:
>>> Also they tend to be used more by 'non-programmers' who get away with
>>> 'stupid' stuff like calling out to system() and piping a bunch of
>>> commands together because they don't know how to use the language
>>> properly :)
>
> On Mon, 11 Jul 2011, cbulist at gmail.com wrote:
>
>> I understand your point but I don't share it.... There are a lot
>> Asterisk-Perl project working in production environment.
>
> Then I didn't communicate my point clearly.
>
> I'm not disparaging Perl programmers or the language. I'm just saying it
> is easier to 'abuse' a language like Perl or PHP than C.
>
> This Bash snippet was posted to the -users list a couple of years ago.
> I'm not trying to embarrass the original programmer or trash his skills,
> I'm just using this snippet as an example. We've all got skeletons in our
> closets :)
>
>> while read line; do
>> epoch=`echo $line | cut -d '|' -f 1`
>> if [ $epoch -ge $start_epoch -a $epoch -le $end_epoch ]; then
>> echo $line
>> fi
>> done < /var/log/asterisk/queue_log
>
> Note the second line. It creates a couple (2 or 3) of processes to
> extract the first field from the queue_log file. For every line in the
> input file!
>
> This kind of coding is easy in a scripting language. It would be way more
> difficult in C. So much more difficult, a programmer with the requisite
> skills to do it should recognize the inefficiency and do it another way.
>
> If the original programmer had a better grasp of the language, he could
> have coded this line as:
>
> epoch=${line:0:10}
>
> Since the Epoch will be 10 digits for the next 300 years, I'd feel
> relatively comfortable with this solution.
For the record, I suspect you meant:
epoch=${line%%|*}
This actually does exactly what is written on the above command (without
having to assume the length of the field), and also does not use
bash-specific syntax and hence can be used with a faster shell like
dash (which Debian and Ubuntu have as /bin/sh by default).
>
> This single change reduced the execution time of his script by an order
> of magnitude.
>
> Recoding it in a language more appropriate to processing lots of data
> (like C) would reduce the execution time to 1/3,000th of the original.
> And yes, I did it and measured it.
Actually, that mistake is easy to make in shell scripts. But even simple
script languages such as awk fare much better here:
awk -F'|' "{
epoch=\$1;
if (epoch <= $start_epoch && epoch >= $end_epoch) {
print epoch
}
}" /var/log/asterisk/queue_log
How much time will it take you to write something as fast as this in C?
--
Tzafrir Cohen
icq#16849755 jabber:tzafrir.cohen at xorcom.com
+972-50-7952406 mailto:tzafrir.cohen at xorcom.com
http://www.xorcom.com iax:guest at local.xorcom.com/tzafrir
More information about the asterisk-users
mailing list