{"id":1382,"date":"2014-07-28T14:28:01","date_gmt":"2014-07-28T06:28:01","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1382"},"modified":"2014-07-28T14:47:17","modified_gmt":"2014-07-28T06:47:17","slug":"processing-performance-test-1","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2014\/07\/28\/processing-performance-test-1\/","title":{"rendered":"Processing Performance Test 1"},"content":{"rendered":"<p>I try to compare various methods to handle mainly image-based computer graphics in the Processing environment and publish the results for developers&#8217; reference. The first one is a very straightforward test by comparing two ways to modify all pixels in a single <a href=\"http:\/\/processing.org\/reference\/PImage.html\">PImage<\/a> object instance.<\/p>\n<p>The first way is nested loops for x and y dimensions and the second way is to traverse the whole <a href=\"http:\/\/processing.org\/reference\/PImage_pixels.html\">pixels<\/a> array in one linear loop.<br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\n\/*\r\n  Comparing the use of nested loops and single loop to change\r\n all pixels in a PImage object instance.\r\n *\/\r\nPImage img;\r\n\r\nvoid setup() {\r\n  size(displayWidth, displayHeight);\r\n  img = createImage(width, height, ARGB);\r\n  img.loadPixels();\r\n}\r\n\r\nvoid draw() {\r\n  background(0, 0, 0);\r\n\r\n  color c = color(255, 0, 0);\r\n  long n = System.nanoTime();\r\n  for (int y=0; y<img.height; y++) {\r\n    for (int x=0; x<img.width; x++) {\r\n      img.set(x, y, c);\r\n    }\r\n  }\r\n  img.updatePixels();\r\n  long elapsedTime = (System.nanoTime() - n)\/1000;\r\n  println(\"Nested loop: \" + elapsedTime);\r\n  image(img, 0, 0);\r\n\r\n  c = color(255, 200, 0);\r\n  int cnt = img.pixels.length;\r\n  n = System.nanoTime();\r\n  for (int i=0; i<cnt; i++) {\r\n    img.pixels[i] = c;\r\n  }\r\n  img.updatePixels();\r\n  elapsedTime = (System.nanoTime()-n)\/1000;\r\n  println(\"Single loop: \" + elapsedTime);\r\n  image(img, 0, 0);\r\n  noLoop();\r\n}\r\n<\/pre>\n<p>The result is pretty obvious. The second method is much faster, around 3 times. I perform 10 runs in my 15 inch Macbook Pro. The average microseconds for each method are:<\/p>\n<ul>\n<li>Nested loop: 23,449<\/li>\n<li>Single loop: <strong>7,537<\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I try to compare various methods to handle mainly image-based computer graphics in the Processing environment and publish the results for developers&#8217; reference. The first one is a very straightforward test by comparing two ways to modify all pixels in a single PImage object instance. The first way is nested loops for x and y [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[131,62],"class_list":["post-1382","post","type-post","status-publish","format-standard","hentry","category-testing","tag-performance","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1382"}],"version-history":[{"count":3,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1382\/revisions"}],"predecessor-version":[{"id":1391,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1382\/revisions\/1391"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1382"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}