Skip to content
This repository has been archived by the owner on Nov 11, 2018. It is now read-only.

Optimize ps termlet. Runs in ~1.3sec instead of ~2sec on an AMD E-350. #340

Merged
merged 6 commits into from
Aug 24, 2014
10 changes: 8 additions & 2 deletions data/Termlets/ps
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/bin/bash

lnnb=0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer variable names that can be immediately understood without deciphering abbreviations. How about first_line instead, with the initial value being true?

Also, that being said, can't the two if blocks actually be merged to create a much more concise and elegant termlet? Currently, we're checking whether we are in the header line in two different places using two different methods.

ps "$@" |

while IFS= read -r line; do
# Position of string "PID" in line
pid_index=$(awk -v a="$line" -v b='PID' 'BEGIN{print index(a,b)}')
if [ $lnnb == 0 ]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this if block header followed the style of the one below. Also note http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts, though it shouldn't matter much here.

then
# Position of string "PID" in line
pid_index=$(awk -v a="$line" -v b='PID' 'BEGIN{print index(a,b)}')
lnnb=1
fi

if [[ "$pid_index" -gt 0 ]]; then
# Header line
Expand All @@ -21,4 +26,5 @@ while IFS= read -r line; do
fi

echo -e "$modified_line"
pid_index=0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems to do nothing.

done
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested this extensively and noticed a speedup of at least a factor of 20. Great stuff!