Optimizing script to reduce execution time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimizing script to reduce execution time
# 1  
Old 03-25-2017
Optimizing script to reduce execution time

Code:
AFILENAME=glow.sh
                FILENAME="/${AFILENAME}"
                WIDTHA=$(echo ${FILENAME} | wc -c)
                NTIME=0
                RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1)
                do
                        WIDTHTIMES=$(awk "BEGIN{printf \"%0.f\n\", $WIDTHA * $NTIME}")
                        EMPTY="$(printf 'u1u1.%*s' $WIDTHTIMES)"
                        if [ $NTIME -le $WIDTHA ] ; then
                                echo "$EMPTY"
                                echo "${EMPTY}${eachletter}${EMPTY}"
                                echo "$EMPTY"
                        fi
                        NTIME=$(($NTIME + 1))
                done)
                Agency=$(echo "${RESULTS}" | tr -d '\n')


the above works well for what i need it to do.

However, as many of you more experienced users can see, it is somewhat inefficient. i was wondering if there's a portable way to rewrite this.

the script basically creates a unique identifier based off the name of a file/script.
# 2  
Old 03-25-2017
Everything you can do in bash (faster) that is done in a child process is in red:
Code:
                FILENAME="/${AFILENAME}"
                WIDTHA=$(echo ${FILENAME} | wc -c)  use mapfile instead
                NTIME=0
                RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1)
                do
                        WIDTHTIMES=$(awk "BEGIN{printf \"%0.f\n\", $WIDTHA * $NTIME}")
                        EMPTY="$(printf 'u1u1.%*s' $WIDTHTIMES)"
                        if [ $NTIME -le $WIDTHA ] ; then
                                echo "$EMPTY"
                                echo "${EMPTY}${eachletter}${EMPTY}"
                                echo "$EMPTY"
                        fi
                        NTIME=$(($NTIME + 1))
                done)
                Agency=$(echo "${RESULTS}" | tr -d '\n') 

Okay. It also looks to me like the entire RESULTS=( ) loop construct should be done with one awk invocation.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-26-2017
Wouldn't it be nice if people knew what the desired output was? I can see a long, unstructured (?) sequence of "u1u1"s and long runs of spaces, interspersed with single characters from the original filename. jim mcnamara is certainly right when suspecting all this can be done with one awk, but who knows HOW that would / should be achieved?

And, the resulting "identifier" is 1199 char long - may work in some proprietary systems, but will certainly not be manageable for e.g. file names' in all *nix file systems.
Did you notice that $WIDTHA does NOT represent the file name's length but is one larger?

Last edited by RudiC; 03-26-2017 at 10:10 AM..
This User Gave Thanks to RudiC For This Post:
# 4  
Old 03-26-2017
Hi.

Portable among what hardware / software systems, what languages, what etc.? ... cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 03-26-2017
This gives an identical output - for any purpose whatsoever - in one awk statement:
Code:
awk -vFN="$FILENAME" '
BEGIN   {LFN = length(FN) + 1
         for (i=0; i<LFN-1; i++){EMPTY = sprintf ("u1u1.%*s", i*LFN, "")
                                 printf "%s\n%s%s%s\n%s\n", EMPTY, EMPTY, substr(FN, i+1, 1), EMPTY, EMPTY
                                }
        }
'

These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 03-26-2017
Quote:
Originally Posted by RudiC
This gives an identical output - for any purpose whatsoever - in one awk statement:
Code:
awk -vFN="$FILENAME" '
BEGIN   {LFN = length(FN) + 1
         for (i=0; i<LFN-1; i++){EMPTY = sprintf ("u1u1.%*s", i*LFN, "")
                                 printf "%s\n%s%s%s\n%s\n", EMPTY, EMPTY, substr(FN, i+1, 1), EMPTY, EMPTY
                                }
        }
'

this is good enough for me. thank you very much!!!!
it is perfect!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to reduce the execution time

We are trying to execute below script for finding out the occurrence of a particular word in a log file Need suggestions to optimize the script. Test.log size - Approx to 500 to 600 MB $wc -l Test.log 16609852 Test.log po_numbers - 11 to 12k po's to search $more po_numbers xxx1335... (10 Replies)
Discussion started by: KumarPiyush7225
10 Replies

2. Shell Programming and Scripting

To take script execution time

Hello Guys, I would like to know is there a way to take the script execution time For e.g i am having a script.sh i need to write inside he script.sh like Start time : 10-Mar-2016 02:30:35 all code over here ... End time : 10-Mar-2016 03:30:32 Script start time - 02:30:35 ... (7 Replies)
Discussion started by: Master_Mind
7 Replies

3. Shell Programming and Scripting

Find execution time of script

i am using bash START=$(date +%s) END=$(date +%s) DIFF=$(echo "$END - $START" ) this code is not working (14 Replies)
Discussion started by: rafa_fed2
14 Replies

4. Shell Programming and Scripting

Automation script to reduce the installation time

DELETED. (0 Replies)
Discussion started by: vasuvv
0 Replies

5. Shell Programming and Scripting

execution time / runtime -- bash script please help!

Hello, I'm running a bash script and I'd like to get more accurate a runtime information then now. So far I've been using this method: STARTM=`date -u "+%s"` ......... *script function.... ......... STOPM=`date -u "+%s"` RUNTIMEM=`expr $STOPM - $STARTM` if (($RUNTIMEM>59)); then... (6 Replies)
Discussion started by: TehOne
6 Replies

6. Shell Programming and Scripting

get execution time of a script

Hi, I have a simple question. How can I get the execution time of a script and maybe put it in a variable? Another question. How can I get only time and not date and put it in a variable? I tried something with "date" command but with no success... If someone could help me... (8 Replies)
Discussion started by: Moumou
8 Replies

7. Shell Programming and Scripting

need inputs on how i can change my script to reduce amount of time the script takes

HI , I have a list1 which consists of data that i have to search and a list2 which has the files that need to be searched .So basically i am using list1 on list2 to see if list1 data is present if found replace it .I have written the code using foreach loop for each list .This is taking the... (1 Reply)
Discussion started by: madhul2002
1 Replies

8. Shell Programming and Scripting

time of execution of script

i want to test whether a script has been executed in last 15 days or not....please help how can i do this...is there any copmmand there to know timings of last execution of any script (8 Replies)
Discussion started by: arghya_owen
8 Replies

9. Shell Programming and Scripting

To reduce execution time

Hi All, The below script I run daily and it consumes 2 hours approx. In this I am calling another script and executing the same twice. Is the loop below the cause for the slow process?Is it possible to finetune the program so that it runs in a much faster way? The first script: #!/bin/ksh... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

10. Shell Programming and Scripting

script execution time calculation

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (9 Replies)
Discussion started by: johnsonbryce
9 Replies
Login or Register to Ask a Question

Featured Tech Videos