{"id":1384,"date":"2014-07-28T14:42:21","date_gmt":"2014-07-28T06:42:21","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1384"},"modified":"2014-07-28T14:46:56","modified_gmt":"2014-07-28T06:46:56","slug":"processing-performance-test-2","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2014\/07\/28\/processing-performance-test-2\/","title":{"rendered":"Processing Performance Test 2"},"content":{"rendered":"<p>The second performance test is to compare the performance among three ways to copy from one <a href=\"http:\/\/processing.org\/reference\/PImage.html\">PImage<\/a> to another with exactly the same size. The first method uses the <a href=\"http:\/\/processing.org\/reference\/PImage_copy_.html\">PImage.copy<\/a> function. The second method use the <a href=\"http:\/\/processing.org\/reference\/arrayCopy_.html\">arrayCopy<\/a> function to copy directly from one pixels array to another. The third method uses a linear loop to traverse the <a href=\"http:\/\/processing.org\/reference\/PImage_pixels.html\">pixels<\/a> array one by one.<br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\n\/*\r\n  Comparing the use of PImage.copy, arrayCopy and explicit for loop to copy one PImage\r\n to another.\r\n *\/\r\nPImage img1, img2;\r\n\r\nvoid setup() {\r\n  size(displayWidth, displayHeight);\r\n  img1 = loadImage(\"bigSample.jpg\");\r\n  img2 = createImage(img1.width, img1.height, ARGB);\r\n  img1.loadPixels();\r\n  img2.loadPixels();\r\n}\r\n\r\nvoid draw() {\r\n  background(0, 0, 0);\r\n\r\n  long n = System.nanoTime();\r\n  img2.copy(img1, 0, 0, img1.width, img1.height, 0, 0, img2.width, img2.height);\r\n  img2.updatePixels();\r\n  long elapsedTime = (System.nanoTime() - n)\/1000;\r\n  println(\"PImage.copy: \" + elapsedTime);\r\n  image(img2, 0, 0);\r\n\r\n  n = System.nanoTime();\r\n  arrayCopy(img1.pixels, img2.pixels);\r\n  img2.updatePixels();\r\n  elapsedTime = (System.nanoTime() - n)\/1000;\r\n  println(\"arrayCopy: \" + elapsedTime);\r\n  image(img2, 0, 0);\r\n\r\n  int cnt = img1.width*img1.height;\r\n  n = System.nanoTime();\r\n  for (int i=0; i<cnt; i++) {\r\n    img2.pixels[i] = img1.pixels[i];\r\n  }\r\n  img2.updatePixels();\r\n  elapsedTime = (System.nanoTime() - n)\/1000;\r\n  println(\"For loop copy: \" + elapsedTime);\r\n  image(img2, 0, 0);\r\n  noLoop();\r\n}\r\n<\/pre>\n<p>The results listed here are the average of five runs.<\/p>\n<ul>\n<li>PImage.copy: 44,744<\/li>\n<li>arrayCopy: <strong>1,238<\/strong><\/li>\n<li>For loop copy: 7,885<\/li>\n<\/ul>\n<p>It is pretty obvious the arrayCopy method is the fastest one.<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The second performance test is to compare the performance among three ways to copy from one PImage to another with exactly the same size. The first method uses the PImage.copy function. The second method use the arrayCopy function to copy directly from one pixels array to another. The third method uses a linear loop to [&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-1384","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\/1384","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=1384"}],"version-history":[{"count":5,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":1390,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions\/1390"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}