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 ThulasingamPosted Sep 4, 2006, 12:37 AM
Manoj BistPosted Aug 29, 2006, 9:46 PM
also try to minimize the number of loops
jc mandapatPosted Aug 27, 2006, 2:56 AM
Bhakeeswaran ThulasingamPosted Aug 27, 2006, 2:34 AM
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.