c#測試本機(jī)sql運(yùn)算速度的代碼示例分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection();
SqlCommand comm = new SqlCommand();
DateTime t1, t2;
int count = 10000; //循環(huán)次數(shù)
string times;
conn.ConnectionString = "Data Source=.;Initial Catalog=Server;Integrated Security=True";
comm.CommandText = "insert into test (Cid,Cvalue) values('1','1')"; //數(shù)據(jù)插入
comm.Connection = conn;
Console.WriteLine("開始插入數(shù)據(jù)\r\n開始時間:" +(t1=DateTime.Now).ToLongTimeString());
try
{
conn.Open();
for (int i = 1; i <= count; i++)
{
comm.ExecuteNonQuery(); //執(zhí)行查詢
}
Console.WriteLine("結(jié)束時間:" + (t2 = DateTime.Now).ToLongTimeString());
times = GetTimeSpan(t1, t2).ToString();
Console.WriteLine("持續(xù)時間:" + times.Substring(0, times.LastIndexOf(".") + 4));
Console.WriteLine("本次測試總共對數(shù)據(jù)庫進(jìn)行了" + count + "次數(shù)據(jù)插入操作!");
//comm.CommandText = "delete from test";
//comm.ExecuteNonQuery();
//Console.WriteLine("測試數(shù)據(jù)已刪除");
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
finally
{
comm = null;
conn.Close();
conn.Close();
}
Console.ReadKey();
}
/// <summary>
/// 返回兩個時間對象的時間間隔
/// </summary>
private static TimeSpan GetTimeSpan(DateTime t1, DateTime t2)
{
DateTime t3;
if (DateTime.Compare(t1, t2) == 1)
{
t3 = t1;
t1 = t2;
t2 = t3;
}
return t2.Subtract(t1);
}
}
}
- 使用 BenchmarkDotNet 對 C# 代碼進(jìn)行基準(zhǔn)測試
- C#建立測試用例系統(tǒng)的示例代碼
- 關(guān)于Unity C# Mathf.Abs()取絕對值性能測試詳解
- C#使用base64對字符串進(jìn)行編碼和解碼的測試
- C#使用String和StringBuilder運(yùn)行速度測試及各自常用方法簡介
- 詳解C# WebApi 接口測試工具:WebApiTestClient
- c# 插入數(shù)據(jù)效率測試(mongodb)
- 京東聯(lián)盟C#接口測試示例分享
- C#/.Net 中快速批量給SQLite數(shù)據(jù)庫插入測試數(shù)據(jù)
- C#代碼性能測試類(簡單實用)
- C#控制臺下測試多線程的方法
- c#測試反射性能示例
- C# 單元測試全解析
相關(guān)文章
C#實現(xiàn)Excel動態(tài)生成PivotTable
這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)Excel動態(tài)生成PivotTable的相關(guān)方法,感興趣的小伙伴們可以參考一下2016-04-04RegexOptions.IgnoreCase正則表達(dá)式替換,忽略大小寫
RegexOptions.IgnoreCase正則表達(dá)式替換,忽略大小寫,需要的朋友可以參考一下2013-03-03C#中通過Command模式實現(xiàn)Redo/Undo方案
這篇文章介紹了C#中通過Command模式實現(xiàn)Redo/Undo方案的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06