Comparison of for and foreach performance in C # to improve programming performance

Source: Internet
Author: User
In many cases, for and foreach have the same function. Many people may choose for or foreach based on their preferences. In this test, we try to compare their execution efficiency by testing data. I hope it will help you in their time.
Test environment:
Hardware environment: piii800 + cpu456
Software environment: Visual Studio. NET + C #
Test cases:
Use the result set to store records and initialize adding records to the result set.
Sampling analysis is performed when the number of records is 1000000, and respectively.
Key test comparison Code The functions are as follows:
// Foreach Start Time
Datetime3 = system. datetime. Now. timeofday. tostring ();
Foreach (datarow row in relationdata. Tables [relationdata. relationinfo_table]. Rows)
Buffer = row [relationdata. pk_table_name]. tostring ();
Datetime4 = system. datetime. Now. timeofday. tostring ();
// For start time
Datetime5 = system. datetime. Now. timeofday. tostring ();
For (Int J = 0; j <1000000; j ++)
Buffer = relationdata. Tables [relationdata. relationinfo_table]. Rows [J] [relationdata. pk_table_name]. tostring ();
Datetime6 = system. datetime. Now. timeofday. tostring ();

Test results:
10000 records:
Foreach read time: 16: 29: 34.2577584
Foreach End Time: 16: 29: 34.2677728
For read Start Time: 16: 29: 34.2677728
For read End Time: 16: 29: 34.2878016
100000 records:
Foreach read time: 16: 31: 10.1055808
Foreach End Time: 16: 31: 10.1957104.
For read Start Time: 16: 31: 10.1957104
For read End Time: 16: 31: 10.4460704
1000000 records:
Foreach read time: 16: 33: 12.6217504
Foreach End Time: 16: 33: 13.6332048
For read Start Time: 16: 33: 13.6332048
For read End Time: 16: 33: 18.7906208
Result Analysis:
1) We can see 10000 records
Foreach uses 0.0100144
The for loop uses 0.0300432
The time taken by foreach is exactly 1/3 of the for loop.
2) We can see 100000 records
Foreach uses 0.0901296
The for loop uses 0.2503600
The time taken by foreach is 36% of the for loop.
3) We can see the results of 1000000 records.
Foreach uses 1.0114544
The for loop uses 4.1574160
The time taken by foreach is 25% of the for loop.
By analyzing these test results, we can see that compared with the previous for statement foreach
For better execution efficiency, the average foreach time is only for30%. When both for and foreach can be used through the test results, we recommend using a more efficient foreach.
We found that the time for writing data with for is about 10 times the time for reading data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.