All pastes #628094 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-20 18:48 UTC
rendered paste body


I just thought this was sort of interesting... Here I am trying to read a file and output to the terminal every line with the line number before...
This is a comparison between PHP and BASH to do this task

PHP:
Code:

#!/usr/bin/php -q
<?php
foreach ( file( $filename) as $line_num => $line ) {
    echo ( "$line_num:$line" );
}
?>

BASH:
Code:

#!/bin/bash
i=1
while [ $i -le `wc -l $filename | gawk '{print $1}'` ] ; do
    line=`head -$i $filename | tail -1`
    echo $i:$line
    i=`expr $i + 1`
done

There is also a difference in execution speed.
The PHP version takes more time to load then bash, but executes MUCH faster.