Efficiency of Loop Statements
Can anyone help me with this problem.Let's sat I want to loop 10 employees using the foreach statement.Under the foreach i have 3 functions that computes their wages,tax and deductions.
My question is this.which is more efficient?
1. for every 1 employee, I compute their wages, tax and deductions
Ex.
Foreach (X in dsEmployees)
{
Compute Wages();
Compute Taxes();
Compute Deductions();
}
2. I compute all the wages for all employees, all taxes for all employees and all deductions for all employees
Ex.
Foreach (X in dsEmployees)
{
Compute Wages();
}
Foreach (X in dsEmployees)
{
Compute Taxes();
}
Foreach (X in dsEmployees)
{
Compute Deductions();
}
can anyone help? thanks!

Bhakeeswaran Thulasingam173 reputationPosted Aug 26, 2006, 10:34 PM EDT
The former method you specified loops through the Particular collection once, where as the latter loops thrice. Why should we iterate the collection thrice whereas we can do the same by iterating only once. This itself specifies the efficient and wise use of loops. So perform all those two operations in one loop itself. And i will also recommend you to use 'for' loops instead of 'foreach' because, the 'foreach' in turn has the internal implementation of 'for' loop, which really gives u a performance.
jc mandapat16 reputationPosted Aug 26, 2006, 10:56 PM EDT
Manoj Bist15 reputationPosted Aug 29, 2006, 5:46 PM EDT
also try to minimize the number of loops
Bhakeeswaran Thulasingam173 reputationPosted Sep 3, 2006, 8:37 PM EDT