fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace Bla
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. List<object> stuff = new List<object>();
  13. const int iterNum = 500;
  14. const int modNum = 50000;
  15.  
  16. for (int z = 0; z < 5; z++)
  17. {
  18. TestVertex(iterNum, modNum);
  19. TestVertexImmu(iterNum, modNum);
  20. }
  21.  
  22. Console.ReadKey(true);
  23. }
  24.  
  25. static void TestVertex(int iterNum, int modNum)
  26. {
  27. Stopwatch watch = new Stopwatch();
  28. watch.Start();
  29. for (int i = 0; i < iterNum; i++)
  30. {
  31. Vertex v = new Vertex(new Vector2f(iterNum, iterNum), Color.Red);
  32. for (int k = 0; k < modNum; k++)
  33. v.Position = new Vector2f(modNum, modNum);
  34. }
  35. Console.WriteLine("Vertex: {0} x {1} modified: {2}", iterNum, modNum, watch.Elapsed.TotalMilliseconds);
  36. }
  37.  
  38. static void TestVertexImmu(int iterNum, int modNum)
  39. {
  40. Stopwatch watch = new Stopwatch();
  41. watch.Start();
  42. for (int i = 0; i < iterNum; i++)
  43. {
  44. VertexImmu w = new VertexImmu(new Vector2f(iterNum, iterNum), Color.Red);
  45. for (int k = 0; k < modNum; k++)
  46. w = new VertexImmu(new Vector2f(modNum, modNum), Color.Red);
  47. }
  48. Console.WriteLine("VertexImmu: {0} x {1} modified: {2}", iterNum, modNum, watch.Elapsed.TotalMilliseconds);
  49. }
  50.  
  51. static void AddMemPressure(List<object> stuff)
  52. {
  53. stuff.Clear();
  54. GC.Collect();
  55. for (int i = 0; i < 500000; i++)
  56. stuff.Add(new object());
  57. }
  58.  
  59. /// <summary>
  60. /// Define a point with color and texture coordinates
  61. /// </summary>
  62. [StructLayout(LayoutKind.Sequential)]
  63. public struct Vertex
  64. {
  65. /// <summary>
  66. /// Construct the vertex from its position
  67. /// The vertex color is white and texture coordinates are (0, 0).
  68. /// </summary>
  69. /// <param name="position">Vertex position</param>
  70. public Vertex(Vector2f position) :
  71. this(position, Color.White, new Vector2f(0, 0))
  72. {
  73. }
  74.  
  75. /// <summary>
  76. /// Construct the vertex from its position and color
  77. /// The texture coordinates are (0, 0).
  78. /// </summary>
  79. /// <param name="position">Vertex position</param>
  80. /// <param name="color">Vertex color</param>
  81. public Vertex(Vector2f position, Color color) :
  82. this(position, color, new Vector2f(0, 0))
  83. {
  84. }
  85.  
  86. /// <summary>
  87. /// Construct the vertex from its position and texture coordinates
  88. /// The vertex color is white.
  89. /// </summary>
  90. /// <param name="position">Vertex position</param>
  91. /// <param name="texCoords">Vertex texture coordinates</param>
  92. public Vertex(Vector2f position, Vector2f texCoords) :
  93. this(position, Color.White, texCoords)
  94. {
  95. }
  96.  
  97. /// <summary>
  98. /// Construct the vertex from its position, color and texture coordinates
  99. /// </summary>
  100. /// <param name="position">Vertex position</param>
  101. /// <param name="color">Vertex color</param>
  102. /// <param name="texCoords">Vertex texture coordinates</param>
  103. public Vertex(Vector2f position, Color color, Vector2f texCoords)
  104. {
  105. Position = position;
  106. Color = color;
  107. TexCoords = texCoords;
  108. }
  109.  
  110. /// <summary>
  111. /// Provide a string describing the object
  112. /// </summary>
  113. /// <returns>String description of the object</returns>
  114. public override string ToString()
  115. {
  116. return "[Vertex]" +
  117. " Position(" + Position + ")" +
  118. " Color(" + Color + ")" +
  119. " TexCoords(" + TexCoords + ")";
  120. }
  121.  
  122. /// <summary>2D position of the vertex</summary>
  123. public Vector2f Position;
  124.  
  125. /// <summary>Color of the vertex</summary>
  126. public Color Color;
  127.  
  128. /// <summary>Coordinates of the texture's pixel to map to the vertex</summary>
  129. public Vector2f TexCoords;
  130. }
  131. /// <summary>
  132. /// Define a point with color and texture coordinates
  133. /// </summary>
  134. [StructLayout(LayoutKind.Sequential)]
  135. public struct VertexImmu
  136. {
  137. /// <summary>
  138. /// Construct the vertex from its position
  139. /// The vertex color is white and texture coordinates are (0, 0).
  140. /// </summary>
  141. /// <param name="position">Vertex position</param>
  142. public VertexImmu(Vector2f position) :
  143. this(position, Color.White, new Vector2f(0, 0))
  144. {
  145. }
  146.  
  147. /// <summary>
  148. /// Construct the vertex from its position and color
  149. /// The texture coordinates are (0, 0).
  150. /// </summary>
  151. /// <param name="position">Vertex position</param>
  152. /// <param name="color">Vertex color</param>
  153. public VertexImmu(Vector2f position, Color color) :
  154. this(position, color, new Vector2f(0, 0))
  155. {
  156. }
  157.  
  158. /// <summary>
  159. /// Construct the vertex from its position and texture coordinates
  160. /// The vertex color is white.
  161. /// </summary>
  162. /// <param name="position">Vertex position</param>
  163. /// <param name="texCoords">Vertex texture coordinates</param>
  164. public VertexImmu(Vector2f position, Vector2f texCoords) :
  165. this(position, Color.White, texCoords)
  166. {
  167. }
  168.  
  169. /// <summary>
  170. /// Construct the vertex from its position, color and texture coordinates
  171. /// </summary>
  172. /// <param name="position">Vertex position</param>
  173. /// <param name="color">Vertex color</param>
  174. /// <param name="texCoords">Vertex texture coordinates</param>
  175. public VertexImmu(Vector2f position, Color color, Vector2f texCoords)
  176. {
  177. this.position = position;
  178. this.color = color;
  179. this.texCoords = texCoords;
  180. }
  181.  
  182. /// <summary>
  183. /// Provide a string describing the object
  184. /// </summary>
  185. /// <returns>String description of the object</returns>
  186. public override string ToString()
  187. {
  188. return "[VertexImmu]" +
  189. " Position(" + Position + ")" +
  190. " Color(" + Color + ")" +
  191. " TexCoords(" + TexCoords + ")";
  192. }
  193.  
  194. /// <summary>2D position of the vertex</summary>
  195. public Vector2f Position { get { return Position; } }
  196. private readonly Vector2f position;
  197.  
  198. /// <summary>Color of the vertex</summary>
  199. public Color Color { get { return color; } }
  200. private readonly Color color;
  201.  
  202. /// <summary>Coordinates of the texture's pixel to map to the vertex</summary>
  203. public Vector2f TexCoords { get { return texCoords; } }
  204. private readonly Vector2f texCoords;
  205. }
  206.  
  207. /// <summary>
  208. /// Vector2f is an utility class for manipulating 2 dimensional
  209. /// vectors with float components
  210. /// </summary>
  211. [StructLayout(LayoutKind.Sequential)]
  212. public struct Vector2f
  213. {
  214. /// <summary>
  215. /// Construct the vector from its coordinates
  216. /// </summary>
  217. /// <param name="x">X coordinate</param>
  218. /// <param name="y">Y coordinate</param>
  219. public Vector2f(float x, float y)
  220. {
  221. X = x;
  222. Y = y;
  223. }
  224.  
  225. /// <summary>
  226. /// Operator - overload ; returns the opposite of a vector
  227. /// </summary>
  228. /// <param name="v">Vector to negate</param>
  229. /// <returns>-v</returns>
  230. public static Vector2f operator -(Vector2f v)
  231. {
  232. return new Vector2f(-v.X, -v.Y);
  233. }
  234.  
  235. /// <summary>
  236. /// Operator - overload ; subtracts two vectors
  237. /// </summary>
  238. /// <param name="v1">First vector</param>
  239. /// <param name="v2">Second vector</param>
  240. /// <returns>v1 - v2</returns>
  241. public static Vector2f operator -(Vector2f v1, Vector2f v2)
  242. {
  243. return new Vector2f(v1.X - v2.X, v1.Y - v2.Y);
  244. }
  245.  
  246. /// <summary>
  247. /// Operator + overload ; add two vectors
  248. /// </summary>
  249. /// <param name="v1">First vector</param>
  250. /// <param name="v2">Second vector</param>
  251. /// <returns>v1 + v2</returns>
  252. public static Vector2f operator +(Vector2f v1, Vector2f v2)
  253. {
  254. return new Vector2f(v1.X + v2.X, v1.Y + v2.Y);
  255. }
  256.  
  257. /// <summary>
  258. /// Operator * overload ; multiply a vector by a scalar value
  259. /// </summary>
  260. /// <param name="v">Vector</param>
  261. /// <param name="x">Scalar value</param>
  262. /// <returns>v * x</returns>
  263. public static Vector2f operator *(Vector2f v, float x)
  264. {
  265. return new Vector2f(v.X * x, v.Y * x);
  266. }
  267.  
  268. /// <summary>
  269. /// Operator * overload ; multiply a scalar value by a vector
  270. /// </summary>
  271. /// <param name="x">Scalar value</param>
  272. /// <param name="v">Vector</param>
  273. /// <returns>x * v</returns>
  274. public static Vector2f operator *(float x, Vector2f v)
  275. {
  276. return new Vector2f(v.X * x, v.Y * x);
  277. }
  278.  
  279. /// <summary>
  280. /// Operator / overload ; divide a vector by a scalar value
  281. /// </summary>
  282. /// <param name="v">Vector</param>
  283. /// <param name="x">Scalar value</param>
  284. /// <returns>v / x</returns>
  285. public static Vector2f operator /(Vector2f v, float x)
  286. {
  287. return new Vector2f(v.X / x, v.Y / x);
  288. }
  289.  
  290. /// <summary>
  291. /// Provide a string describing the object
  292. /// </summary>
  293. /// <returns>String description of the object</returns>
  294. public override string ToString()
  295. {
  296. return "[Vector2f]" +
  297. " X(" + X + ")" +
  298. " Y(" + Y + ")";
  299. }
  300.  
  301. /// <summary>X (horizontal) component of the vector</summary>
  302. public float X;
  303.  
  304. /// <summary>Y (vertical) component of the vector</summary>
  305. public float Y;
  306. }
  307.  
  308. /// <summary>
  309. /// Utility class for manipulating 32-bits RGBA colors
  310. /// </summary>
  311. [StructLayout(LayoutKind.Sequential)]
  312. public struct Color
  313. {
  314. /// <summary>
  315. /// Construct the color from its red, green and blue components
  316. /// </summary>
  317. /// <param name="red">Red component</param>
  318. /// <param name="green">Green component</param>
  319. /// <param name="blue">Blue component</param>
  320. public Color(byte red, byte green, byte blue) :
  321. this(red, green, blue, 255)
  322. {
  323. }
  324.  
  325. /// <summary>
  326. /// Construct the color from its red, green, blue and alpha components
  327. /// </summary>
  328. /// <param name="red">Red component</param>
  329. /// <param name="green">Green component</param>
  330. /// <param name="blue">Blue component</param>
  331. /// <param name="alpha">Alpha (transparency) component</param>
  332. public Color(byte red, byte green, byte blue, byte alpha)
  333. {
  334. R = red;
  335. G = green;
  336. B = blue;
  337. A = alpha;
  338. }
  339.  
  340. /// <summary>
  341. /// Construct the color from another
  342. /// </summary>
  343. /// <param name="color">Color to copy</param>
  344. public Color(Color color) :
  345. this(color.R, color.G, color.B, color.A)
  346. {
  347. }
  348.  
  349. /// <summary>
  350. /// Provide a string describing the object
  351. /// </summary>
  352. /// <returns>String description of the object</returns>
  353. public override string ToString()
  354. {
  355. return "[Color]" +
  356. " R(" + R + ")" +
  357. " G(" + G + ")" +
  358. " B(" + B + ")" +
  359. " A(" + A + ")";
  360. }
  361.  
  362. /// <summary>Red component of the color</summary>
  363. public byte R;
  364.  
  365. /// <summary>Green component of the color</summary>
  366. public byte G;
  367.  
  368. /// <summary>Blue component of the color</summary>
  369. public byte B;
  370.  
  371. /// <summary>Alpha (transparent) component of the color</summary>
  372. public byte A;
  373.  
  374.  
  375. /// <summary>Predefined black color</summary>
  376. public static readonly Color Black = new Color(0, 0, 0);
  377.  
  378. /// <summary>Predefined white color</summary>
  379. public static readonly Color White = new Color(255, 255, 255);
  380.  
  381. /// <summary>Predefined red color</summary>
  382. public static readonly Color Red = new Color(255, 0, 0);
  383.  
  384. /// <summary>Predefined green color</summary>
  385. public static readonly Color Green = new Color(0, 255, 0);
  386.  
  387. /// <summary>Predefined blue color</summary>
  388. public static readonly Color Blue = new Color(0, 0, 255);
  389.  
  390. /// <summary>Predefined yellow color</summary>
  391. public static readonly Color Yellow = new Color(255, 255, 0);
  392.  
  393. /// <summary>Predefined magenta color</summary>
  394. public static readonly Color Magenta = new Color(255, 0, 255);
  395.  
  396. /// <summary>Predefined cyan color</summary>
  397. public static readonly Color Cyan = new Color(0, 255, 255);
  398.  
  399. /// <summary>Predefined (black) transparent color</summary>
  400. public static readonly Color Transparent = new Color(0, 0, 0, 0);
  401. }
  402. }
  403. }
  404.  
Time limit exceeded #stdin #stdout 5s 39248KB
stdin
Standard input is empty
stdout
Vertex: 500 x 50000 modified: 363.5466
VertexImmu: 500 x 50000 modified: 898.6707
Vertex: 500 x 50000 modified: 362.8784
VertexImmu: 500 x 50000 modified: 898.445
Vertex: 500 x 50000 modified: 362.4149
VertexImmu: 500 x 50000 modified: 898.6271
Vertex: 500 x 50000 modified: 362.8637
VertexImmu: 500 x 50000 modified: 898.3091
Vertex: 500 x 50000 modified: 363.2264