Skip to content

Instantly share code, notes, and snippets.

@mikeserv
Forked from Artoria2e5/UL238582_linky.bash
Created December 9, 2015 04:32
Show Gist options
  • Save mikeserv/d83dc1b7e19c1c6deadd to your computer and use it in GitHub Desktop.
Save mikeserv/d83dc1b7e19c1c6deadd to your computer and use it in GitHub Desktop.
All those SE trash
#!/bin/bash
n=${1:-1024} TIMEFORMAT=$'real\t%3lR\nuser\t%3lU\nsys\t%3lS\n'
echo "Testing performance on $n random elements... Filling array."
time while ((n--)); do to_sort+=($RANDOM); done
# http://unix.stackexchange.com/a/247659/73443
ul_bub () {
for ((i=0; i <= $((${#arr[@]} - 2)); ++i))
do
for ((j=((i + 1)); j <= ((${#arr[@]} - 1)); ++j))
do
if [[ ${arr[i]} -gt ${arr[j]} ]]
then
# echo $i $j ${arr[i]} ${arr[j]}
tmp=${arr[i]}
arr[i]=${arr[j]}
arr[j]=$tmp
fi
done
done
}
echo >&2 ul_bub
arr=("${to_sort[@]}")
time ul_bub
# http://unix.stackexchange.com/a/247660/73443
sortnums(){
local OLDPWD IFS=' /'
cd -- "$(mktemp -d)" || return
touch -- $*; ls -A >/dev/null
cd - >/dev/null &&
rm -rf -- "$OLDPWD"
}
echo >&2 touch_and_ls
time (sortnums "${to_sort[@]}")
# http://unix.stackexchange.com/a/248137/73443
echo >&2 index_sort
(
time {
declare -A tmp
for i in "${to_sort[@]}"; do (( tmp[$i]++ )); done
# This eats duplicates.
sorted_nodup=( "${!tmp[@]}" )
}
echo >&2 '.. expand_dup (add this to the calculation if you want all elements back)'
time {
for i in "${!tmp[@]}"; do
j=${tmp[$i]}
while ((j--)); do sorted+=("$i"); done
done
}
)
# http://unix.stackexchange.com/a/248145/73443
ext_sort_44() for array do
readarray -tf '' "$array" < <(
eval "printf '%s\0' \"\${$array[@]}\" | sort -zg")
done
echo >&2 ext_sort_44
arr=("${to_sort[@]}")
time ext_sort_44 arr
ext_sort() for array do
tmp=() i=0
while IFS= read -rd '' "tmp[i++]"; do :; done < <(
eval "printf '%s\0' \"\${$array[@]}\" | sort -zg")
eval "$array=("'"${tmp[@]}")'
done
arr=("${to_sort[@]}")
time ext_sort arr
#!/bin/bash
((!EUID)) || exec sudo "$0" "$@" # Addicted to OR-logic
declare -A src_dst_pair src_dst_old; # dictionaries -- the older one not being used
oops(){ printf '%b\n' "oOpsS!\t$1">&2; exit "${2:-1}"; }
## oldlist file
## Gets the contents of a legacy list.
oldlist() {
local arr i s d;
awk '/^#BEGIN-log#/ {flag=1; next} /^#END-log#/ {flag=0} flag' "$1" | mapfile arr
for i in "${arr[@]}"; do
case "$i" in *'=('*=*=*')') :;; *) continue;; esac
i="${i#*(}"
i="${i%)}"
read s d <<< "$i"
remember "${s#*=}" "${d#*=}" src_dst_old
done
}
##remember key val <varname> Generates a basically safe-to-use line of script and appends it to self.
# TODO: write this back to a script.
remember() {
printf '%s\n' "${3:-src_dst_pair}[$(printf %q "$1")]=$(printf %q "$2")" >> "$0"
}
try_link(){
local SRC="$1" DST="$2" src_in_dst
[[ -e "$SRC" ]] || [[ -e "$DST" ]] || oops "$DST"
[[ -e "$SRC" ]] || mv "$DST" "$SRC"
[[ -e "$DST" ]] || ln -Tsf "$SRC" "$DST"
src_in_dst=("$DST"/*"$SRC"*)
((${#src_in_dst[@]})) || ln -Tsf "$SRC" "$DST"
}
# -> for thing in "$@"
for thing; do
[[ $thing == /* ]] || thing="$PWD/$thing" # relative path, hmm.
SRC=/.hd$thing DST=$thing
[[ "$SRC $DST" != *'.hd/.hd'* ]] || oops 'pathdup: .hd/.hd'
remember "$SRC" "$DST"
try_link "$SRC" "$DST"
done
# Another way to spell eval "$(blah)"
source <(awk '/^# __DATA_START__/ { flag=1; next } flag')
for those in "${!src_dst_pair[@]}"; do try_link "$those" "${src_dst_pair[$those]}"; done
exit
# __DATA_START__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment