By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

C# Performance Test

P: 2
Hi All,

We are facing performance issues using C# program and norrowed down to very simple program. Here is program and its output as well. Can you please have a look and explain me why there is periodic delay of 15 milliseconds?

Please run it on your system and see if you observe same delay ( may be some policy on my system causing it to sleep for 15 milliseconds). Appreciate your help.

Source Code:
Expand|Select|Wrap|Line Numbers
  1. // PInvokeTest.cs
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5.  
  6. class PlatformInvokeTest
  7. {
  8.     static DateTime stdt;
  9.  
  10.     public static void dateandTime(string str)
  11.     {
  12.         DateTime dt = DateTime.Now;
  13.         TimeSpan t = dt.Subtract(stdt);
  14.         Console.WriteLine("difference from previous print : "+t.Milliseconds.ToString("000"));
  15.  
  16.         stdt = dt;
  17.         if (str != null)
  18.         {
  19.             Console.WriteLine(str + dt.Hour + ":" + dt.Minute + ":" + dt.Second + ":" + dt.Millisecond.ToString("000"));
  20.         }
  21.         else
  22.         {
  23.             Console.WriteLine(dt.Hour + ":" + dt.Minute + ":" + dt.Second + ":" + dt.Millisecond.ToString("000"));
  24.         }
  25.     }
  26.     public static void abc(string str1)
  27.     {
  28.         if(str1 !=null)
  29.         {
  30.  
  31.         }
  32.     }
  33.  
  34.     public static void Main() 
  35.     {
  36.  
  37.         stdt = DateTime.Now;
  38.  
  39.         int i = 0;
  40.         while (i < 100)
  41.         {
  42.              dateandTime("Before ");
  43.             //abc("abc");
  44.              dateandTime("After ");
  45.  
  46.             i++;
  47.         }
  48.     }
  49. }
Output with 100:
Expand|Select|Wrap|Line Numbers
  1. difference from previous print : 000
  2. Before 14:0:21:359
  3. difference from previous print : 000
  4. After 14:0:21:359
  5. difference from previous print : 000
  6. Before 14:0:21:359
  7. difference from previous print : 000
  8. After 14:0:21:359
  9. difference from previous print : 000
  10. Before 14:0:21:359
  11. difference from previous print : 000
  12. After 14:0:21:359
  13. difference from previous print : 000
  14. Before 14:0:21:359
  15. difference from previous print : 000
  16. After 14:0:21:359
  17. difference from previous print : 000
  18. Before 14:0:21:359
  19. difference from previous print : 000
  20. After 14:0:21:359
  21. difference from previous print : 000
  22. Before 14:0:21:359
  23. difference from previous print : 000
  24. After 14:0:21:359
  25. difference from previous print : 000
  26. Before 14:0:21:359
  27. difference from previous print : 000
  28. After 14:0:21:359
  29. difference from previous print : 000
  30. Before 14:0:21:359
  31. difference from previous print : 000
  32. After 14:0:21:359
  33. difference from previous print : 000
  34. Before 14:0:21:359
  35. difference from previous print : 000
  36. After 14:0:21:359
  37. difference from previous print : 000
  38. Before 14:0:21:359
  39. difference from previous print : 000
  40. After 14:0:21:359
  41. difference from previous print : 000
  42. Before 14:0:21:359
  43. difference from previous print : 000
  44. After 14:0:21:359
  45. difference from previous print : 000
  46. Before 14:0:21:359
  47. difference from previous print : 000
  48. After 14:0:21:359
  49. difference from previous print : 000
  50. Before 14:0:21:359
  51. difference from previous print : 000
  52. After 14:0:21:359
  53. difference from previous print : 000
  54. Before 14:0:21:359
  55. difference from previous print : 000
  56. After 14:0:21:359
  57. difference from previous print : 000
  58. Before 14:0:21:359
  59. difference from previous print : 000
  60. After 14:0:21:359
  61. difference from previous print : 000
  62. Before 14:0:21:359
  63. difference from previous print : 000
  64. After 14:0:21:359
  65. difference from previous print : 000
  66. Before 14:0:21:359
  67. difference from previous print : 000
  68. After 14:0:21:359
  69. difference from previous print : 000
  70. Before 14:0:21:359
  71. difference from previous print : 000
  72. After 14:0:21:359
  73. difference from previous print : 000
  74. Before 14:0:21:359
  75. difference from previous print : 000
  76. After 14:0:21:359
  77. difference from previous print : 000
  78. Before 14:0:21:359
  79. difference from previous print : 000
  80. After 14:0:21:359
  81. difference from previous print : 000
  82. Before 14:0:21:359
  83. difference from previous print : 000
  84. After 14:0:21:359
  85. difference from previous print : 000
  86. Before 14:0:21:359
  87. difference from previous print : 000
  88. After 14:0:21:359
  89. difference from previous print : 000
  90. Before 14:0:21:359
  91. difference from previous print : 000
  92. After 14:0:21:359
  93. difference from previous print : 000
  94. Before 14:0:21:359
  95. difference from previous print : 000
  96. After 14:0:21:359
  97. difference from previous print : 000
  98. Before 14:0:21:359
  99. difference from previous print : 000
  100. After 14:0:21:359
  101. difference from previous print : 000
  102. Before 14:0:21:359
  103. difference from previous print : 000
  104. After 14:0:21:359
  105. difference from previous print : 000
  106. Before 14:0:21:359
  107. difference from previous print : 000
  108. After 14:0:21:359
  109. difference from previous print : 000
  110. Before 14:0:21:359
  111. difference from previous print : 000
  112. After 14:0:21:359
  113. difference from previous print : 000
  114. Before 14:0:21:359
  115. difference from previous print : 000
  116. After 14:0:21:359
  117. difference from previous print : 000
  118. Before 14:0:21:359
  119. difference from previous print : 000
  120. After 14:0:21:359
  121. difference from previous print : 000
  122. Before 14:0:21:359
  123. difference from previous print : 000
  124. After 14:0:21:359
  125. difference from previous print : 000
  126. Before 14:0:21:359
  127. difference from previous print : 000
  128. After 14:0:21:359
  129. difference from previous print : 000
  130. Before 14:0:21:359
  131. difference from previous print : 000
  132. After 14:0:21:359
  133. difference from previous print : 000
  134. Before 14:0:21:359
  135. difference from previous print : 000
  136. After 14:0:21:359
  137. difference from previous print : 000
  138. Before 14:0:21:359
  139. difference from previous print : 000
  140. After 14:0:21:359
  141. difference from previous print : 000
  142. Before 14:0:21:359
  143. difference from previous print : 000
  144. After 14:0:21:359
  145. difference from previous print : 000
  146. Before 14:0:21:359
  147. difference from previous print : 000
  148. After 14:0:21:359
  149. difference from previous print : 000
  150. Before 14:0:21:359
  151. difference from previous print : 000
  152. After 14:0:21:359
  153. difference from previous print : 000
  154. Before 14:0:21:359
  155. difference from previous print : 000
  156. After 14:0:21:359
  157. difference from previous print : 000
  158. Before 14:0:21:359
  159. difference from previous print : 000
  160. After 14:0:21:359
  161. difference from previous print : 000
  162. Before 14:0:21:359
  163. difference from previous print : 000
  164. After 14:0:21:359
  165. difference from previous print : 000
  166. Before 14:0:21:359
  167. difference from previous print : 000
  168. After 14:0:21:359
  169. difference from previous print : 000
  170. Before 14:0:21:359
  171. difference from previous print : 000
  172. After 14:0:21:359
  173. difference from previous print : 000
  174. Before 14:0:21:359
  175. difference from previous print : 000
  176. After 14:0:21:359
  177. difference from previous print : 000
  178. Before 14:0:21:359
  179. difference from previous print : 000
  180. After 14:0:21:359
  181. difference from previous print : 000
  182. Before 14:0:21:359
  183. difference from previous print : 000
  184. After 14:0:21:359
  185. difference from previous print : 000
  186. Before 14:0:21:359
  187. difference from previous print : 000
  188. After 14:0:21:359
  189. difference from previous print : 000
  190. Before 14:0:21:359
  191. difference from previous print : 000
  192. After 14:0:21:359
  193. difference from previous print : 000
  194. Before 14:0:21:359
  195. difference from previous print : 000
  196. After 14:0:21:359
  197. difference from previous print : 000
  198. Before 14:0:21:359
  199. difference from previous print : 000
  200. After 14:0:21:359
  201. difference from previous print : 000
  202. Before 14:0:21:359
  203. difference from previous print : 000
  204. After 14:0:21:359
  205. difference from previous print : 000
  206. Before 14:0:21:359
  207. difference from previous print : 000
  208. After 14:0:21:359
  209. difference from previous print : 000
  210. Before 14:0:21:359
  211. difference from previous print : 000
  212. After 14:0:21:359
  213. difference from previous print : 000
  214. Before 14:0:21:359
  215. difference from previous print : 000
  216. After 14:0:21:359
  217. difference from previous print : 000
  218. Before 14:0:21:359
  219. difference from previous print : 000
  220. After 14:0:21:359
  221. difference from previous print : 000
  222. Before 14:0:21:359
  223. difference from previous print : 000
  224. After 14:0:21:359
  225. difference from previous print : 000
  226. Before 14:0:21:359
  227. difference from previous print : 000
  228. After 14:0:21:359
  229. difference from previous print : 000
  230. Before 14:0:21:359
  231. difference from previous print : 000
  232. After 14:0:21:359
  233. difference from previous print : 000
  234. Before 14:0:21:359
  235. difference from previous print : 000
  236. After 14:0:21:359
  237. difference from previous print : 000
  238. Before 14:0:21:359
  239. difference from previous print : 000
  240. After 14:0:21:359
  241. difference from previous print : 000
  242. Before 14:0:21:359
  243. difference from previous print : 000
  244. After 14:0:21:359
  245. difference from previous print : 000
  246. Before 14:0:21:359
  247. difference from previous print : 000
  248. After 14:0:21:359
  249. difference from previous print : 000
  250. Before 14:0:21:359
  251. difference from previous print : 000
  252. After 14:0:21:359
  253. difference from previous print : 000
  254. Before 14:0:21:359
  255. difference from previous print : 000
  256. After 14:0:21:359
  257. difference from previous print : 000
  258. Before 14:0:21:359
  259. difference from previous print : 000
  260. After 14:0:21:359
  261. difference from previous print : 000
  262. Before 14:0:21:359
  263. difference from previous print : 000
  264. After 14:0:21:359
  265. difference from previous print : 000
  266. Before 14:0:21:359
  267. difference from previous print : 000
  268. After 14:0:21:359
  269. difference from previous print : 000
  270. Before 14:0:21:359
  271. difference from previous print : 000
  272. After 14:0:21:359
  273. difference from previous print : 000
  274. Before 14:0:21:359
  275. difference from previous print : 000
  276. After 14:0:21:359
  277. difference from previous print : 000
  278. Before 14:0:21:359
  279. difference from previous print : 000
  280. After 14:0:21:359
  281. difference from previous print : 000
  282. Before 14:0:21:359
  283. difference from previous print : 000
  284. After 14:0:21:359
  285. difference from previous print : 000
  286. Before 14:0:21:359
  287. difference from previous print : 000
  288. After 14:0:21:359
  289. difference from previous print : 000
  290. Before 14:0:21:359
  291. difference from previous print : 000
  292. After 14:0:21:359
  293. difference from previous print : 000
  294. Before 14:0:21:359
  295. difference from previous print : 000
  296. After 14:0:21:359
  297. difference from previous print : 000
  298. Before 14:0:21:359
  299. difference from previous print : 000
  300. After 14:0:21:359
  301. difference from previous print : 000
  302. Before 14:0:21:359
  303. difference from previous print : 000
  304. After 14:0:21:359
  305. difference from previous print : 000
  306. Before 14:0:21:359
  307. difference from previous print : 000
  308. After 14:0:21:359
  309. difference from previous print : 000
  310. Before 14:0:21:359
  311. difference from previous print : 000
  312. After 14:0:21:359
  313. difference from previous print : 000
  314. Before 14:0:21:359
  315. difference from previous print : 000
  316. After 14:0:21:359
  317. difference from previous print : 000
  318. Before 14:0:21:359
  319. difference from previous print : 000
  320. After 14:0:21:359
  321. difference from previous print : 000
  322. Before 14:0:21:359
  323. difference from previous print : 000
  324. After 14:0:21:359
  325. difference from previous print : 000
  326. Before 14:0:21:359
  327. difference from previous print : 000
  328. After 14:0:21:359
  329. difference from previous print : 000
  330. Before 14:0:21:359
  331. difference from previous print : 000
  332. After 14:0:21:359
  333. difference from previous print : 000
  334. Before 14:0:21:359
  335. difference from previous print : 000
  336. After 14:0:21:359
  337. difference from previous print : 000
  338. Before 14:0:21:359
  339. difference from previous print : 000
  340. After 14:0:21:359
  341. difference from previous print : 000
  342. Before 14:0:21:359
  343. difference from previous print : 000
  344. After 14:0:21:359
  345. difference from previous print : 000
  346. Before 14:0:21:359
  347. difference from previous print : 015
  348. After 14:0:21:375
  349. difference from previous print : 000
  350. Before 14:0:21:375
  351. difference from previous print : 000
  352. After 14:0:21:375
  353. difference from previous print : 000
  354. Before 14:0:21:375
  355. difference from previous print : 000
  356. After 14:0:21:375
  357. difference from previous print : 000
  358. Before 14:0:21:375
  359. difference from previous print : 000
  360. After 14:0:21:375
  361. difference from previous print : 000
  362. Before 14:0:21:375
  363. difference from previous print : 000
  364. After 14:0:21:375
  365. difference from previous print : 000
  366. Before 14:0:21:375
  367. difference from previous print : 000
  368. After 14:0:21:375
  369. difference from previous print : 000
  370. Before 14:0:21:375
  371. difference from previous print : 000
  372. After 14:0:21:375
  373. difference from previous print : 000
  374. Before 14:0:21:375
  375. difference from previous print : 000
  376. After 14:0:21:375
  377. difference from previous print : 000
  378. Before 14:0:21:375
  379. difference from previous print : 000
  380. After 14:0:21:375
  381. difference from previous print : 000
  382. Before 14:0:21:375
  383. difference from previous print : 000
  384. After 14:0:21:375
  385. difference from previous print : 000
  386. Before 14:0:21:375
  387. difference from previous print : 000
  388. After 14:0:21:375
  389. difference from previous print : 000
  390. Before 14:0:21:375
  391. difference from previous print : 000
  392. After 14:0:21:375
  393. difference from previous print : 000
  394. Before 14:0:21:375
  395. difference from previous print : 000
  396. After 14:0:21:375
  397. difference from previous print : 000
  398. Before 14:0:21:375
  399. difference from previous print : 000
  400. After 14:0:21:375
Feb 7 '09 #1
Share this Question
Share on Google+
2 Replies


vekipeki
Expert 100+
P: 229
If your thread has a normal priority, then it is normal that it gets interrupted by the OS every now and then to give other threads some processor time. You can give your thread a higher priority to make those interruptions less frequent, but I believe .Net CLR is not exactly intended for real-time applications.

For example, since String is immutable in C#, and every string addition in C# creates a new String instance, you can see that your "dateandTime" method allocates 14 new strings on every call (if I didn't count it wrong) - one new string for each .ToString(), and one new string for each addition. This means that GC will have to kick in sooner or later to clean up that stuff.

Calling Console.WriteLine also takes some time. You can try creating a static StringBuilder object and append all your strings to it using Append and AppendLine (without any "+" operators), and then write the resulting string to the Console at the end - it would certainly improve performance and decrease the number of unnecessary allocations.

What is the actual problem you would like to solve, that would need 100% of your processor time?
Feb 8 '09 #2

P: 2
Hi Vekipeki,

Application intend to send replies to a communication driver at least 1000 times per second. Communication driver is in C++. In the original code delay of 15 milliseconds is experienced every 7th or 8th call from C# to C++. Data transfer is 248 bytes. Time delay is constant and it is 015 milliseconds. Which is very high for any application to loose ( real , non-real time).
Feb 8 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.