"Performance" test the performance of Mssqlhelper

Source: Internet
Author: User
Tags emit

This article has no technical content, is to test the mssqlhelper in the use of reflection, do not use reflection performance comparison.

After that, don't ask why you don't use ORM--there's another article about your own ORM upgrade history these years.

Background:

I have a mssqlhelper, this helper class is the most basic of a database operations class.

When query queries a collection, you can specify a delegate for the reader = = Object--If you do not specify it, Mssqlhelper automatically assigns values through reflection.

        static void Main (string[] args) {Test0 ();            Test1 ();            Test0 ();            Test1 ();        Console.readkey (); } public static void Test0 () {String connstring = "Data source=localhost;initial CATALOG=INKFXBL og User Id=sa;            pwd=123.com; ";            DateTime TIME0 = DateTime.Now; list<ts_sdk> list = mssqlhelper.query<ts_sdk> (connstring, "SELECT * FROM dbo.            Ts_sdk ");            DateTime time1 = DateTime.Now; Console.WriteLine ("Test0 |" + list. Count + "|" + (TIME1-TIME0).        TotalSeconds + "seconds"); } public static void Test1 () {String connstring = "Data source=localhost;initial CATALOG=INKFXBL og User Id=sa;            pwd=123.com; ";            DateTime TIME0 = DateTime.Now; list<ts_sdk> list = mssqlhelper.query<ts_sdk> (connstring, "SELECT * FROM dbo.    Ts_sdk ", null, reader = = {TS_SDK item = new TS_SDK ();            Item.                 FID = (Int64) reader["FID"]; Item.                 Fnumber = (string) reader["Fnumber"]; Item.                 Fenum = (string) reader["Fenum"]; Item.                 FName = (string) reader["FName"]; Item.                 Fsdkname = (string) reader["Fsdkname"]; Item.                 Ffullname = (string) reader["Ffullname"]; Item.                FType = (string) reader["FType"]; Item.                 Fvisit = (string) reader["Fvisit"]; Item.                 Finheritfrom = (string) reader["Finheritfrom"]; Item.                Fjoinbasetype = (string) reader["Fjoinbasetype"]; Item.                 Fjoinchildtype = (string) reader["Fjoinchildtype"]; Item.                 Fisstatic = (Boolean) reader["fisstatic"]; Item.                 Fisoverride = (Boolean) reader["Fisoverride"]; Item.                 Fisvirtual = (Boolean) reader["Fisvirtual"]; Item.                 Fisabstract = (Boolean) reader["Fisabstract"]; Item.                 Fisinherit = (Boolean) reader["Fisinherit"]; Item. FIsnetfx = (Boolean) reader["Fisnetfx"]; Item.                 Fouturl = (string) reader["Fouturl"]; Item.                 Fsummary = (string) reader["Fsummary"]; Item.                Frtsummary = (string) reader["Frtsummary"]; Item.                 Fcscode = (string) reader["Fcscode"]; Item.                 Fvbcode = (string) reader["Fvbcode"]; Item.                 Fcppcode = (string) reader["Fcppcode"]; Item.                 Ffscode = (string) reader["Ffscode"]; Item.                fassembly = (string) reader["fassembly"]; Item.                 Fversion = (string) reader["Fversion"]; Item.                 Fnamespace = (string) reader["Fnamespace"]; Item.                 Fparentid = (Int64) reader["Fparentid"]; Item.                Fparentnumber = (string) reader["Fparentnumber"]; Item.                 Fdemo = (string) reader["Fdemo"]; Item.                 Finfo = (string) reader["Finfo"];            return item;            });            DateTime time1 = DateTime.Now; Console.WriteLine ("Test1 | "+ list. Count + "|" + (TIME1-TIME0).        TotalSeconds + "seconds"); }

  

Operation Result:

Read the full table 7W row record.

--it is clear that the delegate read by reader is not specified and the built-in reflection code is used for 3 seconds.

--Of course, the Mssqlhelper built-in delegate has stability control, using this type of code:

Item. FID = Tools.tolong (reader["FID"]); item. FName = tools.tostring ("FName");

This type of conversion function, performance is certainly not as fast as the following code:

Item. FID = (Int64) reader["FID"]; Item. FName = (string) reader["FName"];

Why should I insist that Tools provide a strong type of transfer?

> Because of stability

> Wide range of Conversions: compatible with the following types of perverted database data

This is the time format of the browser, the following code can get 2017-03-20 02:46:06 000DateTime Timea = Tools.todatetime ("Mon Mar 02:46:06 gmt+0800 (Chinese Standard Time Room) ");  The following code can be obtained 2017-03-19 01:43:15 000DateTime Timea = Tools.todatetime ("March 19, 2017 01:43 15 seconds");  

  

Let's use the tools type strong-turn function to try and see how much performance is wasted by the type conversion of tools.

        public static void Test2 () {String connstring = "Data source=localhost;initial catalog=inkfxblo G User Id=sa;            pwd=123.com; ";            DateTime TIME0 = DateTime.Now; list<ts_sdk> list = mssqlhelper.query<ts_sdk> (connstring, "SELECT * FROM dbo.                Ts_sdk ", null, reader = = {TS_SDK item = new TS_SDK (); This time the reader delegate,//type conversion using the Tools.tolong () Tools.tolong () Such functions//why not directly with (string) reader["FN Ame "] This kind of direct conversion? -For stability, I would rather sacrifice performance item.                FID = Tools.tolong (reader["FID"]); Item.                Fnumber = tools.tostring (reader["Fnumber"]); Item.                Fenum = tools.tostring (reader["Fenum"]); Item.                FName = tools.tostring (reader["FName"]); Item.                Fsdkname = tools.tostring (reader["Fsdkname"]); Item.                Ffullname = tools.tostring (reader["Ffullname"]); Item.                FType = tools.tostring (reader["FType"]);Item.                Fvisit = tools.tostring (reader["fvisit"]); Item.                Finheritfrom = tools.tostring (reader["Finheritfrom"]); Item.                Fjoinbasetype = tools.tostring (reader["Fjoinbasetype"]); Item.                Fjoinchildtype = tools.tostring (reader["Fjoinchildtype"]); Item.                Fisstatic = Tools.toboolean (reader["fisstatic"]); Item.                Fisoverride = Tools.toboolean (reader["fisoverride"]); Item.                Fisvirtual = Tools.toboolean (reader["fisvirtual"]); Item.                Fisabstract = Tools.toboolean (reader["fisabstract"]); Item.                Fisinherit = Tools.toboolean (reader["Fisinherit"]); Item.                Fisnetfx = Tools.toboolean (reader["Fisnetfx"]); Item.                Fouturl = tools.tostring (reader["Fouturl"]); Item.                Fsummary = tools.tostring (reader["fsummary"]); Item.                Frtsummary = tools.tostring (reader["frtsummary"]); Item.                Fcscode = tools.tostring (reader["Fcscode"]); Item. FVBCode = tools.tostring (reader["Fvbcode"]); Item.                Fcppcode = tools.tostring (reader["Fcppcode"]); Item.                Ffscode = tools.tostring (reader["Ffscode"]); Item.                fassembly = tools.tostring (reader["fassembly"]); Item.                Fversion = tools.tostring (reader["fversion"]); Item.                Fnamespace = tools.tostring (reader["Fnamespace"]); Item.                Fparentid = Tools.tolong (reader["Fparentid"]); Item.                Fparentnumber = tools.tostring (reader["Fparentnumber"]); Item.                Fdemo = tools.tostring (reader["Fdemo"]); Item.                Finfo = tools.tostring (reader["Finfo"]);            return item;            });            DateTime time1 = DateTime.Now; Console.WriteLine ("Test2 |" + list. Count + "|" + (TIME1-TIME0).        TotalSeconds + "seconds"); }

  

Test again:

--recently want to put their bottom-level auxiliary class again optimization, so the code turned out, toss a bit ~

--------------------------------------------------------------------------------------------------------------- -------------------------

Just modified the reflection code, enabled the Emit high-speed reflection

Performance has been significantly improved, performance only lost 15 ~20%--I'm already satisfied.

Ps. Current Emit reflection uses the common code (inside and outside an auxiliary class)--if the targeted write Emit code, performance can be promoted again, but I am lazy to write.

--------------------------------------------------------------------------------------------------------------- -------------------------

Added one of the most native notation--the performance limit

        public static void Testsouce () {String connstring = "Data source=localhost;initial CATALOG=INKF Xblog; User Id=sa;            pwd=123.com; ";            DateTime TIME0 = DateTime.Now;            List<ts_sdk> list =new list<ts_sdk> (); using (SqlConnection conn = new SqlConnection (connstring)) {Conn.                Open (); using (SqlCommand cmd = conn. CreateCommand ()) {cmd.commandtext = "select * FROM dbo.                    Ts_sdk "; using (SqlDataReader reader = cmd. ExecuteReader ()) {while (reader.                            Read ()) {TS_SDK item = new TS_SDK (); Item.                            FID = (Int64) reader["FID"]; Item.                            Fnumber = (string) reader["Fnumber"]; Item.                            Fenum = (string) reader["Fenum"]; Item.             FName = (string) reader["FName"];               Item.                            Fsdkname = (string) reader["Fsdkname"]; Item.                            Ffullname = (string) reader["Ffullname"]; Item.                            FType = (string) reader["FType"]; Item.                            Fvisit = (string) reader["Fvisit"]; Item.                            Finheritfrom = (string) reader["Finheritfrom"]; Item.                            Fjoinbasetype = (string) reader["Fjoinbasetype"]; Item.                            Fjoinchildtype = (string) reader["Fjoinchildtype"]; Item.                            Fisstatic = (Boolean) reader["fisstatic"]; Item.                            Fisoverride = (Boolean) reader["Fisoverride"]; Item.                            Fisvirtual = (Boolean) reader["Fisvirtual"]; Item.                            Fisabstract = (Boolean) reader["Fisabstract"]; Item.                            Fisinherit = (Boolean) reader["Fisinherit"]; Item.                            Fisnetfx = (Boolean) reader["Fisnetfx"]; Item. Fouturl = (string) reader["Fouturl"]; Item.                            Fsummary = (string) reader["Fsummary"]; Item.                            Frtsummary = (string) reader["Frtsummary"]; Item.                            Fcscode = (string) reader["Fcscode"]; Item.                            Fvbcode = (string) reader["Fvbcode"]; Item.                            Fcppcode = (string) reader["Fcppcode"]; Item.                            Ffscode = (string) reader["Ffscode"]; Item.                            fassembly = (string) reader["fassembly"]; Item.                            Fversion = (string) reader["Fversion"]; Item.                            Fnamespace = (string) reader["Fnamespace"]; Item.                            Fparentid = (Int64) reader["Fparentid"]; Item.                            Fparentnumber = (string) reader["Fparentnumber"]; Item.                            Fdemo = (string) reader["Fdemo"]; Item.                            Finfo = (string) reader["Finfo"]; List.  ADD (item);                      }}}} DateTime time1 = DateTime.Now; Console.WriteLine ("testsouce |" + list. Count + "|" + (TIME1-TIME0).        TotalSeconds + "seconds"); }

  

"Performance" test the performance of Mssqlhelper

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.