This repository has been archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Optimize ps termlet. Runs in ~1.3sec instead of ~2sec on an AMD E-350. #340
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9b8530
Optimize ps termlet. Runs in ~1.3sec instead of ~2sec on an AMD E-350.
lwandrebeck 21c077b
Optimize ps termlet. Runs in less than half a second instead of ~2sec…
lwandrebeck 37d35e1
Merge branch 'termlets' of github.com:lwandrebeck/finalterm into term…
lwandrebeck 318e4b0
Optimize ls termlet a bit. 11 sec for ~1000 files instead of 22 on an…
lwandrebeck 74eb2eb
Remove a useless line in ps termlet.
lwandrebeck 30cf259
Merge remote-tracking branch 'upstream/master' into termlets
lwandrebeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
#!/bin/bash | ||
|
||
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 [[ "$pid_index" -gt 0 ]]; then | ||
# Header line | ||
pid_end=$(($pid_index + 3)) | ||
modified_line="$line" | ||
else | ||
# Content line | ||
left_part=${line:0:pid_end-1} | ||
pid_part=${left_part##* } | ||
# Remove pid_part from left_part | ||
left_part=${left_part:0:${#left_part}-${#pid_part}} | ||
right_part=${line:pid_end-1} | ||
modified_line="$left_part$(text_menu_start '3')$pid_part$(text_menu_end '3')$right_part" | ||
fi | ||
# IFS is '\n' | ||
IFS=$'\012' | ||
psoutput=($(ps "$@")) | ||
# Just in case PPID could be displayed before PID, search for ' PID' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it ever possible that "PID" stands at the beginning of the line, or is preceded by a tab rather than space? If yes, this could fail... |
||
pid_index=$(awk -v a="${psoutput[0]}" -v b=' PID' 'BEGIN{print index(a,b)}') | ||
# don’t use $() in loops, as it spawns a sub-process, so get markings out of the loop. | ||
begin_mark=$(text_menu_start '3') | ||
end_mark=$(text_menu_end '3') | ||
# last character position of PID display. | ||
let pid_end=pid_index+4 | ||
# display 1st line as is, then destroy it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting idea. I like it! |
||
echo -e ${psoutput[0]} | ||
unset psoutput[0] | ||
|
||
for line in ${psoutput[@]}; do | ||
# Content line | ||
left_part=${line:0:${pid_end}-1} | ||
pid_part=${left_part##* } | ||
# Remove pid_part from left_part | ||
left_part=${left_part:0:${#left_part}-${#pid_part}} | ||
right_part=${line:pid_end-1} | ||
modified_line="$left_part$begin_mark$pid_part$end_mark$right_part" | ||
echo -e "$modified_line" | ||
done | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe bash will actually recognize
IFS='\n'
directly