Bash - array loop performance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - array loop performance
# 1  
Old 10-01-2014
Bash - array loop performance

Hi,

another little question...

"sn" is an array whose elements can vary from about 55,000 to about 150,000 elements. Each element consists of an integer between 0-255, eg: ${sn[1]} contain the value: 103 . For a decrypt-procedure I need scroll all the elements 4 or 5 times. Here is an example of one of the loop where n is length of sn array:
Code:
 for(( i=0; i<n-2; i++ ))
 do sn[$i]=$((${sn[$i]} ^ ${sn[$i+2]} ^ (${sn[$i+1]} * k4) % 256))
 done

 for(( i=(n-1); i>1; i--))
 do sn[$i]=$(( ${sn[$i]} ^ ${sn[$i-2]} ^ (${sn[$i-1]} * k3) % 256))
 done

It all works ... but it takes 3 minutes to loop even when the equivalent vb6 takes me globally no more than 10 seconds.
Code:
For i = 1 To n - 2: sn(i) = sn(i) Xor sn(i + 2) Xor (k4 * sn(i + 1)) Mod 256: Next

I'm doing something wrong? The script is successful without any errors.
I had tried some things... for example work in "do" section with small array in this way:
Code:
a=${sn[@]:i:3};
IFS=' ' read -a an <<< "$a"
x=$an{[0]};
y=$an{[1]};
z=$an{[2]};
sn[i]=$(( x ^ z ^ (y*4) % 256 ))

but seems doesn't change much. Any idea how can I increase my loop performance?

thank you
math
# 2  
Old 10-01-2014
maybe it's not the language construct so much as it's the hardware. Do you mean Visual Basic. What are the physical machines?
This User Gave Thanks to blackrageous For This Post:
# 3  
Old 10-01-2014
I don't think bash is too good at maths, even though you are dealing with integer math only...
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-01-2014
Hi, thank you for reply.
It's the same machine (P4) that had XP sp3 (32bit), and now run with only ubunto server (without X... and I don't want install WINE).

product: Intel(R) Pentium(R) 4 CPU 2.66GHz
vendor: Intel Corp.
size: 2700MHz
width: 32 bits
ram: 1.5gb


The vb6 prog was a simple .bas service and was very fast.
The bash conversion that I made is very fast everywhere but in loops menthioned is damn slow. Same machine.

---------- Post updated at 03:08 PM ---------- Previous update was at 02:50 PM ----------

Quote:
Originally Posted by RudiC
I don't think bash is too good at maths, even though you are dealing with integer math only...
Mmm I'm newbie in Linux, do you suggest rewrite the entire script in some else? Smilie

What could I use that is already installed in ubuntu-server? ..and preferably that allows me to import a little 'bash code already written?

NOTE: I had try now to remove all math expression/logic code inside each (5) loops present in the script. Execution time it's now about 5 secs!

Last edited by math4; 10-01-2014 at 05:23 PM..
# 5  
Old 10-01-2014
Use awk or perl its good for maths, please show a representative sample of input, desired output with brief description .
# 6  
Old 10-01-2014
Bash isn't very efficient looking up large arrays I found using bash 4 assoicative arrays gave me a significant boost in speed if you have bash 4 consider using them:

Code:
declare -A sn
n=80000
k4=2
k3=6
for((i=0;i<n;i++))
do  sn[i]=$((RANDOM%256))
done

for(( i=0; i<n-2; i++ ))
do sn[i]=$(( (sn[i] ^ sn[i+1] ^ sn[i+2] * k4) % 256 ))
done

for(( i=(n-1); i>1; i--))
do sn[i]=$(( (sn[i] ^ sn[i-2] ^ sn[i-1] * k3) % 256))
done

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 10-01-2014
Quote:
Originally Posted by Akshay Hegde
Use awk or perl its good for maths, please show a representative sample of input, desired output with brief description .
Brief:
I read a binary file encrypted, parse char by char loaded using a key, and then, with the result, I create a new file readable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

Bash Array connectin to another Array

Hello, i have a script that i need account_number to match a name. for exsample : ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8) and i run a loop curl requst with this the issue is that i want to know on which account were talking about so bash will know this : ... (4 Replies)
Discussion started by: batchenr
4 Replies

3. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

4. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

5. UNIX for Advanced & Expert Users

How to test RAID10 array performance [Debian Wheezy]?

I have created a RAID10 array (near layout) with four 8 GiB virtual hard drives, making a 16 GiB array (/dev/md0 formatted as ext4). On the other hand, I have a 16 GiB RAID1 array (/dev/md1, also formatted as ext4). The purpose of these setups is to compare the read and write performances of each... (13 Replies)
Discussion started by: gacanepa
13 Replies

6. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

7. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

8. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

9. Shell Programming and Scripting

problem access array outside of loop in bash

Below is a test script I was trying to use so that I could understand why the logic was not working in a larger script. While accessing and printing array data inside the while loop, everything is fine. Outside the loop, i guess everything is null?? The for loop that is meant to cycle... (4 Replies)
Discussion started by: adlmostwanted
4 Replies

10. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies
Login or Register to Ask a Question