Skip to main content
added 15 characters in body
Source Link

Interestingly enough, no one actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

For other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

, which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=i-i+1i+=1 (works exactly like (()) with varname expansion, ie. no $ needed) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, no one actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

For other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

, which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=i-i+1 (works exactly like (()) with varname expansion) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, no one actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

For other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

, which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=1 (works exactly like (()) with varname expansion, ie. no $ needed) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, nooneno one actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

or forFor other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

Which, which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=i-i+1 (works exactly like (()) with varname expansion) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, noone actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

or for other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

Which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=i-i+1 (works exactly like (()) with varname expansion) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, no one actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

For other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

, which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=i-i+1 (works exactly like (()) with varname expansion) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

added 55 characters in body
Source Link

Interestingly enough, noone actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

or for other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

Which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=1i+=i-i+1 (works exactly like (()) with varname expansion) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, noone actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

or for other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

Which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=1 instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

Interestingly enough, noone actually ever mentioned the truly fastest way to do loops in bash:

x=4000000

declare -i i=0
while ((i++ < x)); do # or ((++1<x))
  :
done

or for other than +1 iterations (slightly slower) use:

x=4000000

declare -i i=-1
while (((i += 1) < x)); do
  :
done

Which is about 17% faster than the C-like for loop.

Also in general, if you need to do easy math outside a loop condition, always declare -i all used variables first. Then you can use simple i+=i-i+1 (works exactly like (()) with varname expansion) instead of ((i+=1)). Never use let +=1 - it's always gonna be the slowest (just like declare i+=1).

added 1 character in body
Source Link
Loading
added 267 characters in body
Source Link
Loading
added 15 characters in body
Source Link
Loading
added 3 characters in body
Source Link
Loading
added 6 characters in body
Source Link
Loading
added 36 characters in body
Source Link
Loading
added 2 characters in body
Source Link
Loading
added 107 characters in body
Source Link
Loading
Source Link
Loading