[Test Analysis] For and Foreach Perform Efficiency Test Results

zhaozj2021-02-17  58

In many cases, For and foreach have the same function, choose for or foreach may all be read, this test is trying to pass the test data to compare their execution efficiency. I hope to bring some help to everyone.

Test Environment: Hardware Environment: PIII800 CPU456 Software Environment: VisualStudio.net C #

Test case: Use the results set to store records, initialization to the results set add records to the record number of 10,000, 100,000, 1000000 records, respectively, sample analysis

The key test comparison code is as follows: // foreach start time DateTime3 = system.datetime.now.timeofday.tostring (); foreach (data (DataRow Row in RelationData.tables [rateData.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 = rateData.tables [rateData.relationInfo_table]. Rows [J] [RelationData. PK_TABLE_NAME] .tostring (); datetime6 = system.datetime.now.timeofday.tostring (); Test Result: 10000 Record: Foreach Read Time: 16: 29: 34.2577584 Foreach End Time: 16: 29: 34.2677728 for reading Start time: 16: 29: 34.2677728 for reading End Time: 16: 29: 34.2878016 100000 records: Foreach Read Time: 16: 31: 10.1055808 Foreach End Time: 16: 31: 10.1957104 for reading start time: 16: 31: 10.1957104 For reading: 16: 31: 10.4460704 1000000 Record: Foreach Read Time: 16: 33: 12.6217504 Foreach End Time: 16: 33: 13.6332048 for Read Start Time: 16: 33: 13.6332048 for reading End Time: 16: 33: 18.7906208

Results Analysis: 1) For 10000 records, it can be seen that foreach uses 0.0100144 for loop for the time spent in the 0.0300432 foreach. It is exactly the 1/3 2 of the for cycle. The time spent in 0.2503600 foreach is 36% 3 for circulating 3) For 1000000 record results, it can be seen that Foreach has used 1.0114544 for cycles. 4.1574160 Foreach spends 25% of For cycle.

转载请注明原文地址:https://www.9cbs.com/read-29114.html

New Post(0)