BASH for C programmers:

Read a text file word-by-word in BASH:
#!/bin/bash
for WORD in cat filename
do
echo $WORD done  Read a text file line-by-line in BASH: #!/bin/bash while read LINE do echo$LINE
done < filename

Looping in BASH:
i=0
while [ $i -lt 10 ] do echo$i

# next
i=$(($i+1))
done

Update: another way to loop suggested in the comment.

## Optimizing for Performance

Recently, I was exploring on how to optimize my program. According to my research, there are three types of optimization:
1. Design Level Optimization
2. Code Level Optimization
3. Performance Level Optimization
Design Level Optimization is a kind of optimization that focuses on the design of your program. A program that is written very well with good design that minimizes the complexity, robust, correct data structure and correct algorithm.

Code Level Optimization is optimization that goes down into the code / assembly code level. You really try to squeeze tiny little bit of code here and there. This kind of optimization will carefully consider how to loop, how to branch, etc2. For example, you will think which any of the following code is faster:

int arr[10]; int i;
for (i=0; i<sizeof(arr); ++i) arr[i] = i;

or

int arr[10]; int *p;
for (p=arr; p<arr+sizeof(arr) ; ++p) *p = i;

It turns out that this level of optimization does not work anymore for modern processor and optimizing compiler such as Visual Studio .NET 2005. I tried both in Visual Studio (Release Mode); run few million times and found no difference in speed. So, for most of you that still think about this kind of optimization, forget it. It is not going to bring any significant performance boost.

Note: I tried to compile and run the code in SPARC machine and it turns out the second one produce faster result.

Performance Level Optimization is optimization that considers about the usage of cache, data size and code tuning with tools. I haven't tried this one since I am still beginner in this optimization area. But many people has pointed out that this is the kind of optimization that will bring significant speed gain and it is IMPOSSIBLE to do it without profiler.