Test environment:
Hardware: Intel I5 4 core +8g memory.
Software: Windows 7 + vs2013 SP2 + EF6.1 + MVC5.1
Database: vs2013 comes with SQL Express 2012.
Test process:
1. Create a new MVC project for the default template, add the following code to the models and create the corresponding library table:
[Table (" Person")] Public classPerson { Public intId {Get;Set; } Public stringUserName {Get;Set; } PublicDateTime Regtime {Get;Set; } PublicDateTime Birthdate {Get;Set; } Public stringMark {Get;Set; } }
Here is an interesting episode, the beginning of the error can not find "ado.people", wondering, how EF so "smart", automatically think the table name is corresponding to the person people?
Regardless, use the properties of [Table] to force the hold.
2. Create a new two action in HomeController, one that is inserted into the person and one for the person:
usingEfxpoadocompare.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Common;usingSystem.Data.SqlClient;usingSystem.Diagnostics;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespaceefxpoadocompare.controllers{ Public classHomecontroller:controller { PublicActionResult Index () {returnView (); } PublicActionResult About () {Viewbag.message="Your Application description page."; returnView (); } PublicActionResult Contact () {Viewbag.message="Your Contact page."; returnView (); } /// <summary> ///using EF to save data/// </summary> /// <param name= "persons" ></param> voidEfsave (person[] persons) {Applicationdbcontext db=NewApplicationdbcontext (); Db. Persons.addrange (Persons); Db. SaveChanges (); } /// <summary> ///save data with ADO/// </summary> /// <param name= "persons" ></param> voidAdosave (person[] persons) {SqlConnection conn=NewApplicationdbcontext (). Database.connection asSqlConnection; Conn. Open (); using(SqlTransaction trans =Conn. BeginTransaction ()) {SqlCommand cmd=NewSqlCommand (@"INSERT into person (birthdate,regtime,username,mark) VALUES (@Birthdate, @RegTime, @UserName, @Mark)", Conn, trans); Cmd. Parameters.Add ("Birthdate", System.Data.SqlDbType.Date); Cmd. Parameters.Add ("Regtime", System.Data.SqlDbType.DateTime); Cmd. Parameters.Add ("UserName", System.Data.SqlDbType.NVarChar); Cmd. Parameters.Add ("Mark", System.Data.SqlDbType.NVarChar); foreach(varPinchpersons) {cmd. parameters["Birthdate"]. Value =p.birthdate; Cmd. parameters["Regtime"]. Value =P.regtime; Cmd. parameters["UserName"]. Value =P.username; Cmd. parameters["Mark"]. Value =P.mark; Cmd. ExecuteNonQuery (); } trans.commit (); } conn. Close (); } /// <summary> ///EF Querying Data/// </summary> /// <param name= "persons" ></param> voidEfsearch (person[] persons) {Applicationdbcontext db=NewApplicationdbcontext (); varList = db. Persons.where (p = P.username.compareto ("D") >=1). Take (_num). ToList (); Viewbag.list=list; } /// <summary> ///ADO Querying Data/// </summary> /// <param name= "persons" ></param> voidAdosearch (person[] persons) {SqlConnection conn=NewApplicationdbcontext (). Database.connection asSqlConnection; Conn. Open (); stringsql ="SELECT TOP"+ _num +"* From the person WHERE UserName >= ' d '"; SqlCommand Comm=NewSqlCommand (SQL, conn); List<Person> PS =NewList<person>(); using(SqlDataReader reader =Comm. ExecuteReader ()) { while(reader. Read ()) {PS. ADD (NewPerson () {Birthdate= (DateTime) reader["Birthdate"], Id= (int) reader["Id"], UserName= (string) reader["UserName"], Mark= (string) reader["Mark"], Regtime= (DateTime) reader["Regtime"], }); }} viewbag.list=PS; Conn. Close (); } PublicActionResult Searchperson (int?num) { if(num = =NULL) num = +; _num=Num. Value; Personscreater PC=NewPersonscreater (_num); Pc. Onpersonssaving=Adosearch; Viewbag.adoms=pc. Savepersons (); Pc. Onpersonssaving=Efsearch; VIEWBAG.EFMS=pc. Savepersons (); returnView (); } int_num; PublicActionResult Createperson (int?num) { if(num = =NULL) num = +; _num=Num. Value; Personscreater PC=NewPersonscreater (_num); Pc. Onpersonssaving=Adosave; Viewbag.adoms=pc. Savepersons (); Pc. Onpersonssaving=Efsave; VIEWBAG.EFMS=pc. Savepersons (); returnView (); } } classPersonscreater {int_num; PublicPersonscreater (intnum) {_num =num;} PublicAction<person[]>onpersonssaving; Person[] Createpersons () {person[] persons=NewPerson[_num]; Random Rand=NewRandom (); for(inti =0; i < _num; i++) {Persons[i]=NewPerson () {Birthdate= DateTime.Today.AddDays (-rand. Next (365* -,365* -)), Regtime= DateTime.Now.AddSeconds (-rand. Next (0,86400*365)), UserName=Guid.NewGuid (). ToString (), Mark="My Mark:"+Guid.NewGuid (). ToString (),}; } returnpersons; } Stopwatch SW=NewStopwatch (); Public Longsavepersons () {varPersons =createpersons (); Sw. Restart (); onpersonssaving (persons); Sw. Stop (); returnSW. Elapsedmilliseconds; } }}
3. Add a view, configure the route (no table here, focus is not here) try to run through.
4. Start the test:
Test results (Insert data, Unit: MS):
Article number |
100 |
1000 |
3000 |
10000 |
100000 |
Ef |
31 |
196 |
649 |
2955 |
22714 |
Ado |
12 |
128 |
245 |
1044 |
11051 |
Test results (query data, Unit ms):
Article number |
100 |
1000 |
3000 |
10000 |
100000 |
Ef |
4 |
11 |
24 |
50 |
522 |
Ado |
3 |
6 |
13 |
30 |
169 |
I started with the debug version, debugging in the VS environment, the result is less than the release version of debugging, the speed is at least 10 times times slower. So everyone in the production environment deployment, be sure to use relase compile your program, remember to remember.
The above data are the results of the operation under release.
In the results of the above query data, the smaller the data volume, the more irregular the test result, and sometimes even the EF is faster than ADO.
Test conclusion: The difference between EF and ADO is negligible in the typical small data volume application. EF performance lags significantly behind traditional ADO only when data is inserted at scale.