Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- : ${_PROF_INIT:=-1}
- : ${_PROF_START:=0}
- : ${_PROF_STDOUT:=1}
- function __log() {
- local fmt=$1
- if [ $DEBUG -eq 1 ]; then
- shift 1
- if [ "x${fmt//\\n}" == "x${fmt}" ]; then
- fmt="${fmt}\n";
- fi
- printf "${fmt}" "$@" 1>&2
- fi
- }
- function _profiler_calc () {
- echo "$@" | bc -l | xargs printf "%7.3f\n"
- }
- function _profiler_ts() {
- python -c'import time; print repr(time.time())'
- }
- function _profiler_mark() {
- _PROF_START=$(_profiler_ts)
- if [ "x${_PROF_INIT}" == "x-1" ]; then
- _PROF_INIT=${_PROF_START}
- fi
- }
- function _profiler_elapsed() {
- local ARGS=$@
- NOW="$(_profiler_ts)"
- ELAPSED=$(_profiler_calc "${NOW} - ${_PROF_START}")
- ELAPSED_INIT=$(_profiler_calc "${NOW} - ${_PROF_INIT}")
- if [ ${_PROF_STDOUT} -eq 1 ]; then
- printf "%s\n" "$ELAPSED [${ELAPSED_INIT}] ${ARGS}"
- else
- __log "%s\n" "$ELAPSED [${ELAPSED_INIT}] ${ARGS}"
- fi
- }
- function __hash_init() {
- local name=$1
- if [ $(hash_size ${name}) -eq 0 ]; then
- __log "%s\n" "INIT: [$name]"
- eval __hash_${name}___ptr="__NULL__"
- fi
- }
- function hash_put() {
- local name=$1 key=$2 val=$3 exist=0 ptr
- key=$(echo "${key}" | sed -e 's/(\(.*\))/\1/g' | tr -d ' ')
- #key=$(echo "${key}" | md5)
- __hash_init ${name}
- if [ "x$key" == "x__auto" ]; then
- ptr=$(hash_get ${name} __ptr)
- if [ "x${ptr}" == "x__NULL__" ]; then
- random=1
- #random=${RANDOM}
- #random=$(od -vAn -N4 -tu4 < /dev/random)
- ptr=${random}
- else
- let ptr+=1;
- fi
- key=$ptr
- __log "%s\n" "AUTO: [$name] key=$key"
- eval __hash_${name}___ptr=${ptr}
- fi
- __log "%s\n" "PUT : [$name] key=$key value=$val"
- eval __hash_${name}_${key}=${val}
- }
- function hash_get() {
- local name=$1 key=$2 _var var
- key=$(echo "${key}" | sed -e 's/(\(.*\))/\1/g' | tr -d ' ')
- #key=$(echo "${key}" | md5)
- _var=__hash_${name}_${key}
- var=${!_var}
- __log "%s\n" "FIND: [$name] key=$key ret=$var {${_var}}"
- echo ${var}
- }
- function hash_clear() {
- local name=$1 res
- res=$(compgen -A variable __hash_${name} | xargs)
- __log "%s\n" "CLR : name=${name} [$res]"
- unset $res
- }
- function hash_keys() {
- local name=$1
- list=$(compgen -A variable __hash_${name} | sed -e "s/__hash_${name}_//" | grep -v "__ptr")
- __log "=======> LIST: %s\n" $list
- echo $list
- }
- function hash_size() {
- local name=$1
- val=$(hash_keys ${name} | wc -l)
- echo $val
- }
- function hash_print() {
- local name=$1
- __log "%s\n" "PRN : [${name}]"
- for key in $(hash_keys ${name}); do
- hash_get ${name} "${key}"
- done
- }
- : ${DEBUG:=0}
- hash_put lookup "entry1" 1
- hash_put lookup "__empty__" 13
- _profiler_mark
- for ((i=0; i<4000; i++)); do
- hash_put testtable "key_${i}_entry" $i
- done
- _profiler_elapsed "insert testtable"
- hash_size testtable
- #while IFS=\; read key val _ _; do
- # __log "%s\n" "READ: key=$key"
- # if [ "x$key" == "x" ]; then
- # key="__empty__"
- # fi
- # idx=$(hash_get lookup ${key})
- # hash_put table_visitas_$idx 19 "$val"
- #done < <(echo ";param2;param3;param4")
- #for key in $(hash_keys lookup); do
- # idx=$(hash_get lookup "$key")
- # #hash_size table_visitas_$idx
- # printf "=======> FOUND [table_visitas_$idx]: %s\n" $(hash_print table_visitas_$idx)
- #done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement