大叔也说并行和串行`性能提升N倍(N由操作系统位数和cpu核数决定)

时间:2022-02-19 19:01:31

返回目录

并行是.net4.5主打的技术,同时被封装到了System.Threading.Tasks命名空间下,对外提供了静态类Parallel,我们可以直接使用它的静态方法,它可以并行一个委托数组,或者一个IEnumerable的迭代,而今天主要通过一个访问数据库的代码,来说说并发Parallel对整个程序的提升是那么大,我们主要从数据库连接数和程序运行时间上就可以很清晰的看到效果。

简单的测试代码

       [TestMethod]
public void Read()
{
Stopwatch sw = new Stopwatch();
sw.Start();
var actions = new List<Action>(); for (int i = ; i < ; i++)
{
actions.Add(() =>
{
using (var db = new am20160316Entities())
{
var repository = new Lind.DDD.Repositories.EF.EFRepository<ad_contract>(db);
var list = repository.GetModel().ToList();
}
});
}
Parallel.Invoke(actions.ToArray());
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

上面代码为并行执行[可用的线程数据并行](并不是说1000个任务,就必须开1000个线程去干这事,由于.net自己也线程池的概念,所有它会根据系统使用情况去为我们这个进程分配,如果你的各方面请求需要.net分配更多的线程,那就才会现时有多个线程启动(如果是连接数据库,它和你数据连接串的MaxPoolSize和MinPoolSize都是关系的)。

程序与服务器之前的连接

程序与数据库进行通讯时,同时开了一些连接,我们可以通过netstat -nba | findstr 192.168.2.123指令来查看与指定服务器通讯的情况,当我们修改MinPooSize的时候,会影响这个程序与数据库建立通讯的次数,当然同时建立通讯次数越多,性能越好,当然这也要看数据库服务器的能力了,呵呵。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjsAAAC9CAIAAADawivgAAAb80lEQVR4nO2dW5LlLA6EWVov3Uubh3/a4UYoSQlsIE5+DxU+XFIpoVPEXKpd/vz5U4QQQoj90Y0lhBDiDHRjCSGEOAPdWEIIIc5AN5YQQogz0I0lhBDiDHRjCSGEOAPdWEIIIc5AN5YQQogzuG+s60FO6rl3RGcuwEk032hS+xQhyrnOJzL4dfAEx6Pn+vD7A7URvV8yuV8+oDiD+rP8zM1XlPL3x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rxnGxqYX4nejzVoEnQSGGT0R9Z3Pybidv14zV2NL2wGwO/0ibdyVr5RnUpzz/b4D2u1wLyeIzbH3BFgS6QIqPnT6tP8Lgeh92OlTTL6TFysn1tfTLdhV9GD87Lmx/m83oY5R74OOX0mLtaf64c/L3C4pI59vnb7LfmXpiVct1CdcVlIS4wIiMuntga9H6t7VCP6dvxKfRsTRQs1ZfTgPHFyvOvqe36tT/BKPl++f7zxKtl07h9Q2baDeEtT5Gp9X/AyXj8Xl9myDL0fy5tN6wPxXBESRQudTlSkSiH6XEmFLL3Hr/UJWIxLEdJndO6P98/nw1Z94lkCJr3Dbc5ifWDJ8wnMR+Pucgp6Pxa/nhTxjvl+bvYTr5/bMp6XN2W/CVfrN84UV+/xa33ircc6b0Rptk1zDR/3JSpLYBYMXgZP30vf24JDY8FmXhsegd6PdZGLeX2m7chAU/ww46E6hEqaG/8e9Qmj42Ux0g+Vwv2cqMPbdIvGtApYn2gJXj8kwqS2Br0fqznizeKUsU5Uf5YfZjwa1+rw0bH+Kvi6zToXpiaM/il+SM37I467BGu1cPXxRgZLF9LnReyyTepfit6Pxa1vbmH0QQjPbdrPc5zPy8Lk1d2Ix3kzHxDNF9cTiOApL4TndrqfhE+PaF6z4r6KrU+zdN6WatB7xuLfxL0et1RTfyX6Ny+EEEKcgW4sIYQQZ6AbSwghxBnoxhJCCHEGurGEEEKcgW4sIYQQZ6AbSwghxBnoxhJCCHEGurGEEEKcgW4sIYQQZ6AbSwghxBH8Dx0axYHEte6YAAAAAElFTkSuQmCC" alt="" />

事实上,如果我们的程序是单线程串行执行的,上面这个图只要显示一个活动的连接,性能与同时10几个连接,那肯定是没法比的,这个很容易理解,就像先10万的人过桥一下,大桥和独木桥来说,10万人通过的话,时间一定是不一样的,呵呵。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAABZCAIAAADqycycAAAGn0lEQVR4nO2dzWsbRxiH85fsP2EMPvmU5LCRj8nRhkAuARMk1afklOYWyCUbgalp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IFSgBgoBYj5D7GnbNdHr5VoAAAAAElFTkSuQmCC" alt="" />

上面是并行处理1000次连接的时间,3427毫秒,而对于单线程串行来说,我们看一下它的处理时间

代码

       [TestMethod]
public void SignalRead()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = ; i < ; i++)
{ using (var db = new am20160316Entities())
{
var repository = new Lind.DDD.Repositories.EF.EFRepository<ad_contract>(db);
var list = repository.GetModel().ToList();
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

与数据库建立单个连接

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiUAAAA3CAIAAABcjq0FAAAGSUlEQVR4nO2cQZbUMAxEczSOxtGyZMmC+8CKeSG2yiXbrUwz/y94aUcplWR368H0cPz6+eP379+/fv749fPH9+/fv337dgAAAGyHeQMAABUwbwAAoALmDQAAVMC8AQCACpg3AABQAfMGAAAqYN4AAEAF0bw5L8wpX59d0dmLcJKtN1vU52lClvd1vpHFt0MkuJ597hzWb2ibMfqQmfvwEc1Z1N/lZ2+9b0l33lwrd7owfBsUtzLKNdzXlM+J85cS95VrdLZIZUVeHZ8V36if/byLHplwlUqdUvYzRp8Pc58bfmey+rv87K33XWnnza3m7kvRyu5icR+n/XyS/d5lY28562pZhVfHZ3mqn9vrMgW35D0vROLO9Yq+k1fr7/XjePs/Gc6bG91WHr1Oif7qLYmCfZ0VP7cYbWkoslJv5Mfsw7T/bveiHq7XVRCfwqxr6MdMtBLp9GHaf9TP68uVElpNv/Csfrue9b/ux/fm+3k/svNG0D2v69fXl9FZ6Xre5Sc6mqb+RHz38SPog5kilTfSb2O05i4/Edn49tkbvh+n/1toHeqk2X2Z6P+tV07Jol2RSKqZbbAQ1/30U8wF61b4ft6SjfPm2PG50z0K1xUh6/hp9YfXbd5hUaJAveKsR32I+ibEu/FRveJZYWboZ3rd108x7E903frxE61EpkwOH3HyXis1qxBh3VvZrWzjh7Wn9mvdj6OTzfJ+bPn5zZXuQXSOVPZEZu8Oz1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x1nP5o1WxLMfL0W6B/H7tmtfnL45+u/ix9T8eKnzPkJr9fD6E60sti6l74u0YfP93zJvzgvDWyL4pZwN7S0zvvuIoy9SRG6n/VzX/bpanLrEI0JquP4I2Xp1P4fFRreiFJHb7X4mfEZk69qV96W0/em2Lnrkthhda/GavOdlxnT1E/D/CwAAQAXMGwAAqIB5AwAAFTBvAACgAuYNAABUwLwBAIAKmDcAAFAB8wYAACpg3gAAQAXMGwAAqIB5AwAAFTBvAACgAuYNAABUwLwBAIAKmDcAAFAB8wYAACpg3gAAQAXMGwAAqIB5AwAAFTBvAACgAuYNAABUwLwBAIAC/gA1oedvtIya5gAAAABJRU5ErkJggg==" alt="" />

程序运行的时间

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOIAAABeCAIAAACxel+OAAAHEElEQVR4nO2dzW/URhiH+Uvmn4gi5cQJOAybIxxBQuKCFCFvyAlOlBsSF8xKUVGrotBDK4VWZrNFRagcqhaFQyw3KtAe2lVBFSCi5mMqJFR68O5kPF/27trefXd/j6LI9r7z2t48eWfseLJHBAATz5FxHwAA+UBTQABoCggwvKZMwfpqEATmq654a0yRYDAL+DRljEVRxLKEYShf9ciUamrN6TLP9B6agpScamqqGYahqq8ZbFI8QGTV9EdWQdRqyn3dvL+VbozbIWtYfuUGIrzEGWN8+aa5IzbPt7rDHGeUjHhQZMjv9KWa6nerplorzrm2xVp0XZpaFS9ySsOTRFIjkURBK6owvxDNsz07o1ZziH2Flzg0PcSvqavOaZpazc6ttTKPNVLW+NKoQs1sfpemsnIXB5r2kG75q6m2IF/VqqmW01UdTSOteSrC7Ey1zlqkYwBJI4haTTbPg7Nc7b5ln6421DVdZHpMEmlbzDzq3qFpD1m3imgqssq6NHUJKgzRpe6eJlXQXMyOFx1VsNm/Rmwu9gayvQrXjYNPQi3YlUctijJh3A7D+1uWPN2Y815CVNMMZWnKvKgx8k6WWpVrNjVuh4cDgKxesp7JAOmiVOew5uXqLjcmmXFUmlzPo2SAphlG11R1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qelAj0cwYyK/K09u2hl/LGMWqETTIo/2MOVjyrQA7REqz2Rr/zKYGuqrpqZMfvwPUEPTmaIcTXNrpxpmXbDGqI9ae6yFplNPydVUDjdN1DCXpqaOnn15KPGkwCRQmqapH7mf48gck0uZY1DLjA/dc+26rBMBE0jJmqrGWDVVg4XDMKmy/EBof5WFplNPaWNT7UPF/TOYczXVqilzT7nOXQVTQMljU1NT4b7BpCEUQf3VFJrOGuVrqsqkaepZ0PKoKkNTUEk1da2qNbWIprKaWlv5KfG8wNipStPUFf8llMstq6bF/YOm0wcePQEEgKaAANAUEACaAgJAU0AAaAoIAE0BAaApIEAlmraCRitKhBBCJI3Dm/fz7bhr2yJaQW9bvxUAGcrXNIlanPNUuG7cXuubF0WRa8u1sJ02bbAGPAUmpWvaDcO1dris1UXVTnNLHMdCCCGSIGiVfTxgGihZ03Z4rR13TU3XwrCbjdS2tIIGQykFDsrUVHbfmqb+UqqATh/YKVfTeC1cltdH0tS0xKqR5hb/djDjVHKln62m3WXOs/JltiRRq9EbkpqRAAhRhabtfkHtm5qjqRDdZT6PG1LAA27vAwJAU0AAaAoIAE0BAaApIAA0BQSApoAA0BQQAJoCAkBTQABoCggATQEBoCkgADQFBPgfQbt9DZqav7AAAAAASUVORK5CYII=" alt="" />

通过上面的截图我们可以看到,串行比并行慢了足足1000多毫秒,这个数值会随着并发量的提升而升高。

所以,在多核时代,在操作系统处理位数高的时代,我们应该尽量多用并行!

感谢各位的阅读!

返回目录