Guest User

Untitled

a guest
Nov 26th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.19 KB | None | 0 0
  1. namespace BookShopSystem.ConsoleClient
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.Entity;
  6. using System.Data.Entity.Infrastructure;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10.  
  11. using BookShopSystem.Data;
  12. using BookShopSystem.Models;
  13.  
  14. public class Measurements
  15. {
  16. public const int TableRows = 1000;
  17. public static void Main()
  18. {
  19. var context = new BookShopContext();
  20. var count = context.Authors.Count();
  21.  
  22. //0. DrawTable
  23. //DrawTable();
  24.  
  25. //1. AuthorNames with Console.WriteLine vs AuthorNames without Console.WriteLine()
  26. //GetAllAuthorNames1(context);
  27.  
  28. //2. ToList vs nothing
  29. //GetAllAuthorNames2(context);
  30.  
  31. //3. First element of materialised with ToList query vs first element as query
  32. //GetAllAuthorNames3(context);
  33.  
  34. //4. Eager loading with Include vs not materialised vs not materialised query
  35. //GetAllAuthorNames4(context);
  36.  
  37. //5. Eager loading with Include and ToList vs ToList
  38. //GetAllAuthorNames5(context);
  39.  
  40. //6. Eager loading with Include and FirstOrDefault vs FirstOrDefault
  41. //GetAllAuthorNames6(context);
  42.  
  43. //7. Eager loading with Include and then foreach vs foreach, both not materialised
  44. //GetAllAuthorNames7(context);
  45.  
  46. //8. Eager loading with Include and then foreach vs foreach, both materialised in the foreach with ToList
  47. //GetAllAuthorNames8(context);
  48.  
  49. //9. Eager loading with Include and then foreach vs foreach , both materialised in the foreach with Count
  50. //GetAllAuthorNames9(context);
  51.  
  52. //10. Eager loading with Include, then foreach and where vs foreach and where
  53. //GetAllAuthorNames10(context);
  54.  
  55. //11. IQueryable Where with ToList vs ToList and IEnumerable Where
  56. //GetAllAuthorNames11(context);
  57.  
  58. //12. IQueryable Where, Select with ToList vs ToList and IEnumerable Where, Select
  59. //GetAllAuthorNames12(context);
  60.  
  61. //13. Eager Loading with Include and foreach with Console.WriteLine vs Lazy loading in foreach with Console.WriteLine
  62. //GetAllAuthorNames13(context);
  63.  
  64. //14. Eager Loading with Select and foreach with Console.WriteLine vs Lazy loading in foreach with Console.WriteLine
  65. //GetAllAuthorNames14(context);
  66.  
  67. //15. Select and OrderBy with ToList materialisation vs materialisation first, then Select, OrderBy. Not an N + 1 problem
  68. //GetAllAuthorNames15(context);
  69.  
  70. //16. Select and OrderBy with ToList materialisation vs materialisation first, then Select, OrderBy. N + 1 problem
  71. //GetAllAuthorNames16(context);
  72.  
  73. //17. Native Select and OrderBy with ToList query vs Select and OrderBy with ToList
  74. //GetAllAuthorNames17(context);
  75.  
  76. //18. Native Select and OrderBy with ToList, printed in foreach vs Select and OrderBy with ToList, printed in foreach
  77. GetAllAuthorNames18(context);
  78. }
  79.  
  80. public static void DrawTable()
  81. {
  82. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  83. {
  84. writer.WriteLine(new string('-', 144));
  85. writer.Write("|# ");
  86. writer.Write("| Table Rows ");
  87. writer.Write("|" + " Iterations".PadRight(18));
  88. writer.Write("|" + " Measured query".PadRight(29));
  89. writer.Write("|" + " Elapsed Time".PadRight(23));
  90. writer.Write("|" + " Measured query".PadRight(29));
  91. writer.WriteLine("|" + " Elapsed Time".PadRight(23) + "|");
  92. writer.WriteLine(new string('-', 144));
  93. }
  94. }
  95.  
  96. public static void GetAllAuthorNames1(BookShopContext context)
  97. {
  98. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  99. {
  100. var sw = new Stopwatch();
  101. var iterationsCount = 100;
  102. sw.Start();
  103. for (var i = 1; i <= iterationsCount; i++)
  104. {
  105. var authors = context.Authors.Select(a=>a.FirstName);
  106. foreach (var author in authors)
  107. {
  108. Console.WriteLine(author);
  109. }
  110. }
  111.  
  112. sw.Stop();
  113. var elapsedWith = sw.Elapsed;
  114.  
  115. sw.Restart();
  116. for (var i = 1; i <= iterationsCount; i++)
  117. {
  118. var authors = context.Authors.Select(a=>a.FirstName);
  119. }
  120.  
  121. sw.Stop();
  122. var elapsedWithout = sw.Elapsed;
  123.  
  124. writer.Write("|1 ");
  125. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  126. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  127. writer.Write("| Select,Console.WriteLine".PadRight(30));
  128. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  129. writer.Write("| Select".PadRight(30));
  130. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  131. writer.WriteLine(new string('-', 144));
  132. }
  133. }
  134.  
  135. public static void GetAllAuthorNames2(BookShopContext context)
  136. {
  137. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  138. {
  139. var sw = new Stopwatch();
  140. var iterationsCount = 10000;
  141. sw.Start();
  142. for (var i = 1; i <= iterationsCount; i++)
  143. {
  144. var authorsCount = context.Authors.ToList().Count;
  145. }
  146.  
  147. sw.Stop();
  148. var elapsedWith = sw.Elapsed;
  149.  
  150. sw.Restart();
  151. for (var i = 1; i <= iterationsCount; i++)
  152. {
  153. var authorsCount = context.Authors.Count();
  154. }
  155.  
  156. sw.Stop();
  157. var elapsedWithout = sw.Elapsed;
  158.  
  159. writer.Write("|2 ");
  160. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  161. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  162. writer.Write("| ToList".PadRight(30));
  163. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  164. writer.Write("| --".PadRight(30));
  165. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  166. writer.WriteLine(new string('-', 144));
  167. }
  168. }
  169.  
  170. public static void GetAllAuthorNames3(BookShopContext context)
  171. {
  172. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  173. {
  174. var sw = new Stopwatch();
  175. var iterationsCount = 1000;
  176. sw.Start();
  177. for (var i = 1; i <= iterationsCount; i++)
  178. {
  179. var author = context.Authors.ToList().FirstOrDefault();
  180. }
  181.  
  182. sw.Stop();
  183. var elapsedWith = sw.Elapsed;
  184.  
  185. sw.Restart();
  186. for (var i = 1; i <= iterationsCount; i++)
  187. {
  188. var author = context.Authors.FirstOrDefault();
  189. }
  190.  
  191. sw.Stop();
  192. var elapsedWithout = sw.Elapsed;
  193.  
  194. writer.Write("|3 ");
  195. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  196. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  197. writer.Write("| ToList, FirstOrDefault".PadRight(30));
  198. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  199. writer.Write("| FirstOrDefault".PadRight(30));
  200. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  201. writer.WriteLine(new string('-', 144));
  202. }
  203. }
  204.  
  205. public static void GetAllAuthorNames4(BookShopContext context)
  206. {
  207. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  208. {
  209. var sw = new Stopwatch();
  210. var iterationsCount = 1000;
  211. sw.Start();
  212. for (var i = 1; i <= iterationsCount; i++)
  213. {
  214. var authors = context.Authors.Include(a=>a.Books).Count();
  215. }
  216.  
  217. sw.Stop();
  218. var elapsedWith = sw.Elapsed;
  219.  
  220. sw.Restart();
  221. for (var i = 1; i <= iterationsCount; i++)
  222. {
  223. var authors = context.Authors.Count();
  224. }
  225.  
  226. sw.Stop();
  227. var elapsedWithout = sw.Elapsed;
  228.  
  229. writer.Write("|4 ");
  230. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  231. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  232. writer.Write("| Include".PadRight(30));
  233. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  234. writer.Write("| --".PadRight(30));
  235. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  236. writer.WriteLine(new string('-', 144));
  237. }
  238. }
  239.  
  240. public static void GetAllAuthorNames5(BookShopContext context)
  241. {
  242. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  243. {
  244. var sw = new Stopwatch();
  245. var iterationsCount = 1000;
  246. sw.Start();
  247. for (var i = 1; i <= iterationsCount; i++)
  248. {
  249. var authors = context.Authors.Include(a => a.Books).ToList();
  250. }
  251.  
  252. sw.Stop();
  253. var elapsedWith = sw.Elapsed;
  254.  
  255. sw.Restart();
  256. for (var i = 1; i <= iterationsCount; i++)
  257. {
  258. var authors = context.Authors.ToList();
  259. }
  260.  
  261. sw.Stop();
  262. var elapsedWithout = sw.Elapsed;
  263.  
  264. writer.Write("|5 ");
  265. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  266. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  267. writer.Write("| Include, ToList".PadRight(30));
  268. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  269. writer.Write("| ToList".PadRight(30));
  270. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  271. writer.WriteLine(new string('-', 144));
  272. }
  273. }
  274.  
  275. public static void GetAllAuthorNames6(BookShopContext context)
  276. {
  277. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  278. {
  279. var sw = new Stopwatch();
  280. var iterationsCount = 1000;
  281. sw.Start();
  282.  
  283. for (var i = 1; i <= iterationsCount; i++)
  284. {
  285. var author = context.Authors.Include(a => a.Books).FirstOrDefault();
  286. }
  287.  
  288. sw.Stop();
  289. var elapsedWith = sw.Elapsed;
  290.  
  291. sw.Restart();
  292. for (var i = 1; i <= iterationsCount; i++)
  293. {
  294. var author = context.Authors.FirstOrDefault();
  295. }
  296.  
  297. sw.Stop();
  298. var elapsedWithout = sw.Elapsed;
  299.  
  300. writer.Write("|5 ");
  301. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  302. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  303. writer.Write("| Include,FirstOrDefault".PadRight(30));
  304. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  305. writer.Write("| FirstOrDefault".PadRight(30));
  306. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  307. writer.WriteLine(new string('-', 144));
  308. }
  309. }
  310.  
  311. public static void GetAllAuthorNames7(BookShopContext context)
  312. {
  313. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  314. {
  315. var sw = new Stopwatch();
  316. var iterationsCount = 1000;
  317. sw.Start();
  318. for (var i = 1; i <= iterationsCount; i++)
  319. {
  320. var authors = context.Authors.Include(a => a.Books);
  321. foreach (var author in authors)
  322. {
  323. var books = author.Books;
  324. }
  325. }
  326.  
  327. sw.Stop();
  328. var elapsedWith = sw.Elapsed;
  329.  
  330. sw.Restart();
  331. for (var i = 1; i <= iterationsCount; i++)
  332. {
  333. var authors = context.Authors;
  334. foreach (var author in authors)
  335. {
  336. var books = author.Books;
  337. }
  338. }
  339.  
  340. sw.Stop();
  341. var elapsedWithout = sw.Elapsed;
  342.  
  343. writer.Write("|7 ");
  344. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  345. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  346. writer.Write("| Include, foreach".PadRight(30));
  347. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  348. writer.Write("| foreach".PadRight(30));
  349. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  350. writer.WriteLine(new string('-', 144));
  351. }
  352. }
  353.  
  354. public static void GetAllAuthorNames8(BookShopContext context)
  355. {
  356. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  357. {
  358. var sw = new Stopwatch();
  359. var iterationsCount = 1000;
  360. sw.Start();
  361. for (var i = 1; i <= iterationsCount; i++)
  362. {
  363. var authors = context.Authors.Include(a => a.Books);
  364. foreach (var author in authors)
  365. {
  366. var books = author.Books.ToList();
  367. }
  368. }
  369.  
  370. sw.Stop();
  371. var elapsedWith = sw.Elapsed;
  372.  
  373. sw.Restart();
  374. for (var i = 1; i <= iterationsCount; i++)
  375. {
  376. var authors = context.Authors;
  377. foreach (var author in authors)
  378. {
  379. var books = author.Books.ToList();
  380. }
  381. }
  382.  
  383. sw.Stop();
  384. var elapsedWithout = sw.Elapsed;
  385.  
  386. writer.Write("|8 ");
  387. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  388. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  389. writer.Write("| Include, foreach with ToList".PadRight(30));
  390. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  391. writer.Write("| foreach with ToList".PadRight(30));
  392. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  393. writer.WriteLine(new string('-', 144));
  394. }
  395. }
  396.  
  397. public static void GetAllAuthorNames9(BookShopContext context)
  398. {
  399. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  400. {
  401. var sw = new Stopwatch();
  402. var iterationsCount = 1000;
  403. sw.Start();
  404. for (var i = 1; i <= iterationsCount; i++)
  405. {
  406. var authors = context.Authors.Include(a => a.Books);
  407. foreach (var author in authors)
  408. {
  409. var bookCount = author.Books.Count;
  410. }
  411. }
  412.  
  413. sw.Stop();
  414. var elapsedWith = sw.Elapsed;
  415.  
  416. sw.Restart();
  417. for (var i = 1; i <= iterationsCount; i++)
  418. {
  419. var authors = context.Authors;
  420. foreach (var author in authors)
  421. {
  422. var bookCount = author.Books.Count;
  423. }
  424. }
  425.  
  426. sw.Stop();
  427. var elapsedWithout = sw.Elapsed;
  428.  
  429. writer.Write("|9 ");
  430. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  431. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  432. writer.Write("| Include, foreach with Count".PadRight(30));
  433. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  434. writer.Write("| foreach with Count".PadRight(30));
  435. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  436. writer.WriteLine(new string('-', 144));
  437. }
  438. }
  439.  
  440. public static void GetAllAuthorNames10(BookShopContext context)
  441. {
  442. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  443. {
  444. var sw = new Stopwatch();
  445. var iterationsCount = 1000;
  446. sw.Start();
  447. for (var i = 1; i <= iterationsCount; i++)
  448. {
  449. var authors = context.Authors.Include(a => a.Books);
  450. foreach (var author in authors)
  451. {
  452. var bookCount = author.Books.Where(b=>b.Edition == EditionType.Gold);
  453. }
  454. }
  455.  
  456. sw.Stop();
  457. var elapsedWith = sw.Elapsed;
  458.  
  459. sw.Restart();
  460. for (var i = 1; i <= iterationsCount; i++)
  461. {
  462. var authors = context.Authors;
  463. foreach (var author in authors)
  464. {
  465. var bookCount = author.Books.Where(b => b.Edition == EditionType.Gold);
  466. }
  467. }
  468.  
  469. sw.Stop();
  470. var elapsedWithout = sw.Elapsed;
  471.  
  472. writer.Write("|10");
  473. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  474. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  475. writer.Write("| Include, foreach with Where".PadRight(30));
  476. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  477. writer.Write("| foreach with Where".PadRight(30));
  478. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  479. writer.WriteLine(new string('-', 144));
  480. }
  481. }
  482.  
  483. public static void GetAllAuthorNames11(BookShopContext context)
  484. {
  485. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  486. {
  487. var sw = new Stopwatch();
  488. var iterationsCount = 1000;
  489. sw.Start();
  490. for (var i = 1; i <= iterationsCount; i++)
  491. {
  492. var authors = context.Authors.Where(a => a.FirstName.Length >= 5).ToList();
  493. }
  494.  
  495. sw.Stop();
  496. var elapsedWith = sw.Elapsed;
  497.  
  498. sw.Restart();
  499. for (var i = 1; i <= iterationsCount; i++)
  500. {
  501. var authors = context.Authors.ToList().Where(a => a.FirstName.Length >= 5);
  502. }
  503.  
  504. sw.Stop();
  505. var elapsedWithout = sw.Elapsed;
  506.  
  507. writer.Write("|11");
  508. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  509. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  510. writer.Write("| Where,ToList".PadRight(30));
  511. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  512. writer.Write("| ToList,Where".PadRight(30));
  513. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  514. writer.WriteLine(new string('-', 144));
  515. }
  516. }
  517.  
  518. public static void GetAllAuthorNames12(BookShopContext context)
  519. {
  520. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  521. {
  522. var sw = new Stopwatch();
  523. var iterationsCount = 1000;
  524. sw.Start();
  525. for (var i = 1; i <= iterationsCount; i++)
  526. {
  527. var authors = context.Authors.Where(a => a.FirstName.Length >= 5).Select(a=>a.LastName).ToList().Count;
  528. }
  529.  
  530. sw.Stop();
  531. var elapsedWith = sw.Elapsed;
  532.  
  533. sw.Restart();
  534. for (var i = 1; i <= iterationsCount; i++)
  535. {
  536. var authors = context.Authors.ToList().Where(a => a.FirstName.Length >= 5).Select(a => a.LastName).Count();
  537. }
  538.  
  539. sw.Stop();
  540. var elapsedWithout = sw.Elapsed;
  541.  
  542. writer.Write("|12");
  543. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  544. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  545. writer.Write("| Where,Select,ToList".PadRight(30));
  546. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  547. writer.Write("| ToList,Where,Select".PadRight(30));
  548. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  549. writer.WriteLine(new string('-', 144));
  550. }
  551. }
  552. public static void GetAllAuthorNames13(BookShopContext context)
  553. {
  554. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  555. {
  556. var sw = new Stopwatch();
  557. var iterationsCount = 100;
  558. sw.Start();
  559. for (var i = 1; i <= iterationsCount; i++)
  560. {
  561. var books = context.Books.Include(a => a.Author);
  562. foreach (var book in books)
  563. {
  564. Console.WriteLine(book.Author.FirstName);
  565. }
  566. }
  567.  
  568. sw.Stop();
  569. var elapsedWith = sw.Elapsed;
  570.  
  571. sw.Restart();
  572. for (var i = 1; i <= iterationsCount; i++)
  573. {
  574. var books = context.Books;
  575. foreach (var book in books)
  576. {
  577. Console.WriteLine(book.Author.FirstName);
  578. }
  579. }
  580.  
  581. sw.Stop();
  582. var elapsedWithout = sw.Elapsed;
  583.  
  584. writer.Write("|13");
  585. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  586. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  587. writer.Write("| Include, Console.WriteLine".PadRight(30));
  588. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  589. writer.Write("| Console.writeLine".PadRight(30));
  590. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  591. writer.WriteLine(new string('-', 144));
  592. }
  593. }
  594.  
  595. public static void GetAllAuthorNames14(BookShopContext context)
  596. {
  597. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  598. {
  599. var sw = new Stopwatch();
  600. var iterationsCount = 100;
  601. sw.Start();
  602. for (var i = 1; i <= iterationsCount; i++)
  603. {
  604. var authors = context.Books.Select(b=>b.Author.FirstName);
  605. foreach (var author in authors)
  606. {
  607. Console.WriteLine(author);
  608. }
  609. }
  610.  
  611. sw.Stop();
  612. var elapsedWith = sw.Elapsed;
  613.  
  614. sw.Restart();
  615. for (var i = 1; i <= iterationsCount; i++)
  616. {
  617. var books = context.Books;
  618. foreach (var book in books)
  619. {
  620. Console.WriteLine(book.Author.FirstName);
  621. }
  622. }
  623.  
  624. sw.Stop();
  625. var elapsedWithout = sw.Elapsed;
  626.  
  627. writer.Write("|14");
  628. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  629. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  630. writer.Write("| Select, Console.WriteLine".PadRight(30));
  631. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  632. writer.Write("| Console.writeLine".PadRight(30));
  633. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  634. writer.WriteLine(new string('-', 144));
  635. }
  636. }
  637.  
  638. public static void GetAllAuthorNames15(BookShopContext context)
  639. {
  640. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  641. {
  642. var sw = new Stopwatch();
  643. var iterationsCount = 1000;
  644. sw.Start();
  645. for (var i = 1; i <= iterationsCount; i++)
  646. {
  647. var authors = context.Books.Select(b => b.Author.FirstName).OrderBy(a => a).ToList();
  648. }
  649.  
  650. sw.Stop();
  651. var elapsedWith = sw.Elapsed;
  652.  
  653. sw.Restart();
  654. for (var i = 1; i <= iterationsCount; i++)
  655. {
  656. var authors = context.Books.ToList().Select(b => b.Author.FirstName).OrderBy(a => a);
  657. }
  658.  
  659. sw.Stop();
  660. var elapsedWithout = sw.Elapsed;
  661.  
  662. writer.Write("|15");
  663. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  664. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  665. writer.Write("| Select,OrderBy,ToList".PadRight(30));
  666. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  667. writer.Write("| ToList,Select,OrderBy".PadRight(30));
  668. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  669. writer.WriteLine(new string('-', 144));
  670. }
  671. }
  672.  
  673. public static void GetAllAuthorNames16(BookShopContext context)
  674. {
  675. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  676. {
  677. var sw = new Stopwatch();
  678. var iterationsCount = 100;
  679. sw.Start();
  680. for (var i = 1; i <= iterationsCount; i++)
  681. {
  682. var authors = context.Books.Select(b => b.Author.FirstName).OrderBy(a => a).ToList();
  683. foreach (var author in authors)
  684. {
  685. Console.WriteLine(author);
  686. }
  687. }
  688.  
  689. sw.Stop();
  690. var elapsedWith = sw.Elapsed;
  691.  
  692. sw.Restart();
  693. for (var i = 1; i <= iterationsCount; i++)
  694. {
  695. var authors = context.Books.ToList().Select(b => b.Author.FirstName).OrderBy(a => a);
  696. foreach (var author in authors)
  697. {
  698. Console.WriteLine(author);
  699. }
  700. }
  701.  
  702. sw.Stop();
  703. var elapsedWithout = sw.Elapsed;
  704.  
  705. writer.Write("|16");
  706. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  707. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  708. writer.Write("| Select,OrderBy,ToList,Print".PadRight(30));
  709. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  710. writer.Write("| ToList,Select,OrderBy,Print".PadRight(30));
  711. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  712. writer.WriteLine(new string('-', 144));
  713. }
  714. }
  715.  
  716. public static void GetAllAuthorNames17(BookShopContext context)
  717. {
  718. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  719. {
  720. var sw = new Stopwatch();
  721. var iterationsCount = 1000;
  722. var sql = @"SELECT Authors.FirstName FROM Books
  723. INNER JOIN Authors
  724. ON Books.AuthorId = Authors.Id
  725. ORDER BY Authors.FirstName ASC";
  726.  
  727. sw.Start();
  728. for (var i = 1; i <= iterationsCount; i++)
  729. {
  730. var authors = context.Books.Select(b => b.Author.FirstName).OrderBy(a => a).ToList();
  731. }
  732.  
  733. sw.Stop();
  734. var elapsedWith = sw.Elapsed;
  735.  
  736. sw.Restart();
  737. for (var i = 1; i <= iterationsCount; i++)
  738. {
  739. var authors = context.Database.SqlQuery<string>(sql).ToList();
  740. }
  741.  
  742. sw.Stop();
  743. var elapsedWithout = sw.Elapsed;
  744.  
  745. writer.Write("|17");
  746. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  747. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  748. writer.Write("|Select,OrderBy,ToList".PadRight(30));
  749. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  750. writer.Write("|Native Select,OrderBy,ToList".PadRight(30));
  751. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  752. writer.WriteLine(new string('-', 144));
  753. }
  754. }
  755.  
  756. public static void GetAllAuthorNames18(BookShopContext context)
  757. {
  758. using (var writer = new StreamWriter("../../../Resources/measurements.txt", true))
  759. {
  760. var sw = new Stopwatch();
  761. var iterationsCount = 100;
  762. var sql = @"SELECT Authors.FirstName FROM Books
  763. INNER JOIN Authors
  764. ON Books.AuthorId = Authors.Id
  765. ORDER BY Authors.FirstName ASC";
  766.  
  767. sw.Start();
  768. for (var i = 1; i <= iterationsCount; i++)
  769. {
  770. var authors = context.Books.Select(b => b.Author.FirstName).OrderBy(a => a).ToList();
  771. foreach (var author in authors)
  772. {
  773. Console.WriteLine(author);
  774. }
  775. }
  776.  
  777. sw.Stop();
  778. var elapsedWith = sw.Elapsed;
  779.  
  780. sw.Restart();
  781. for (var i = 1; i <= iterationsCount; i++)
  782. {
  783. var authors = context.Database.SqlQuery<string>(sql).ToList();
  784. foreach (var author in authors)
  785. {
  786. Console.WriteLine(author);
  787. }
  788. }
  789.  
  790. sw.Stop();
  791. var elapsedWithout = sw.Elapsed;
  792.  
  793. writer.Write("|18");
  794. writer.Write("|".PadRight(5) + TableRows.ToString().PadRight(8));
  795. writer.Write("|".PadRight(5) + iterationsCount.ToString().PadRight(14));
  796. writer.Write("|Select,OrderBy,ToList,Print".PadRight(30));
  797. writer.Write("|".PadRight(5) + elapsedWith.ToString().PadRight(19));
  798. writer.Write("|Native same query,Print".PadRight(30));
  799. writer.WriteLine("|".PadRight(5) + elapsedWithout.ToString().PadRight(19) + "|");
  800. writer.WriteLine(new string('-', 144));
  801. }
  802. }
  803.  
  804. }
  805. }
Add Comment
Please, Sign In to add comment