Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Perl is absurdly faster than Bash. And, for text manipulation, you can actually achieve better performances with Perl than with C, unless you take time to write complex algorithms. Of course, for simple stuff C can be unbeatable.</p> <p>That said, if your "bash" script is not looping, just calling other programs, then there isn't any gain to be had. For example, if your script looks like "<code>cat X | grep Y | tr -f 3-5 | sort | uniq</code>", then most of the time is spent on cat, grep, tr, sort and uniq, NOT on Bash.</p> <p>You'll gain performance if there is any loop in the script, or if you save multiple reads of the same file.</p> <p>You say you cut stuff between two timestamps on a file. Let's say your Bash script looks like this:</p> <pre><code>LINE1=`grep -n TIMESTAMP1 filename | head -1 | cut -d ':' -f 1` LINE2=`grep -n TIMESTAMP2 filename | head -1 | cut -d ':' -f 1` tail +$LINE1 filename | head -$(($LINE2-$LINE1)) </code></pre> <p>Then you'll gain performance, because you are reading the whole file three times: once for each command where "filename" appears. In Perl, you would do something like this:</p> <pre><code>my $state = 0; while(&lt;&gt;) { exit if /TIMESTAMP2/; print $_ if $state == 1; $state = 1 if /TIMESTAMP1/; } </code></pre> <p>This will read the file only once and will also stop once you read TIMESTAMP2. Since you are processing multiple files, you'd use "last" or "break" instead of "exit", so that the script can continue to process the files.</p> <p>Anyway, seeing your script I'm positive you'll gain a lot by rewriting it in Perl. Notwithstanding the loops dealing with file names (whose speed WILL be improved, but is probably insignificant), for each file which is not fully inside or outside scope you do:</p> <ol> <li>Read the WHOLE file to count lines!</li> <li>Do multiple tails on the file</li> <li>Finish by "head" or "tail" the file once again</li> </ol> <p>Furthermore, head your tails. Each time you do that, some piece of code is reading that data. Some of those lines are being read up to 10 times or more!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload