Quantcast
Channel: Answers for "For different texture sizes, which is faster? SetPixel or SetPixels?"
Viewing all articles
Browse latest Browse all 6

Answer by Wolfram

$
0
0
Your second code example is wrong, you are assigning 90000 times the same pixel array to your texture, which doesn't make sense. To answer your question, SetPixel() will be significantly slower, possibly by an order of magnitude or more. You are setting each of the single pixels individually, 90000 times in total, calling a function that needs to access the texture data, compute the exact location of that pixel, and replace it, plus there is the loop overhead of 300x executing the inner loop. On the other hand, SetPixels() is just one call, assinging the whole array, which can just memcopied into the texture data. EDIT: If your question is about how to best initialize the array, a compromize might be feasible: With your first approach you'll need to execute a statement 90000x in two nested loops. With your second approach you'll need to execute a statement 90000x (=filling the color array with its initial value (note, in your current code, the color will be black, since it is not initialized)). Instead, the fastest method (*) will be to create a Color array of size 300, initialize that, and then call SetPixels 300 times, setting one row at a time: for(int a = 0; a < size; a++) texture.SetPixels(0,a,size,1,colors); (\*) EDIT: After doing some benchmarks, you don't gain anything by splitting the initialization into a "one row at a time" loop. So simply create a Color32[size*size] array, initialize it, and use a single call to texture.SetPixels32(). This will be fastest for most texture resolutions, as @Eric5h5 explained.

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>