Skip to content

Instantly share code, notes, and snippets.

@westnordost
Created October 12, 2018 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westnordost/dc4d8cf93a70c0a68853f7c2a545a8a4 to your computer and use it in GitHub Desktop.
Save westnordost/dc4d8cf93a70c0a68853f7c2a545a8a4 to your computer and use it in GitHub Desktop.
Performance test flipping argb arrays
package de.westnordost.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int width = 1024;
int height = 1024;
int repeats = 100;
long totalModuloDivisionTime = 0;
long totalIncrementComparisonTime = 0;
for(int repeat = 0; repeat < repeats; ++repeat)
{
final int[] argb = createRandomArgbArray(width, height);
long time = System.currentTimeMillis();
runModuloDivision(width, height, argb);
totalModuloDivisionTime += System.currentTimeMillis() - time;
time = System.currentTimeMillis();
runIncrementComparison(width, height, argb);
totalIncrementComparisonTime += System.currentTimeMillis() - time;
}
System.out.println("For random bitmaps of "+width+"x"+height+" pixels, the function takes:");
System.out.println("With modulo/division code: " + (totalModuloDivisionTime/repeats) + "ms");
System.out.println("With increment/comparison code: " + (totalIncrementComparisonTime/repeats) + "ms");
System.out.println("Thus, the new code is "+(100*(totalModuloDivisionTime-totalIncrementComparisonTime)/totalModuloDivisionTime)+"% faster");
}
private int[] createRandomArgbArray(int width, int height)
{
final int[] argb = new int[width * height];
Random rand = new Random();
for (int i1 = 0; i1 < argb.length; i1++)
{
argb[i1] = rand.nextInt();
}
return argb;
}
private void runModuloDivision(int width, int height, int[] argb)
{
final int[] abgr = new int[width * height];
int row = 0, col = 0;
for (int i = 0; i < argb.length; i++)
{
col = i % width;
row = i / width;
final int pix = argb[i];
final int pb = (pix >> 16) & 0xff;
final int pr = (pix << 16) & 0x00ff0000;
final int pix1 = (pix & 0xff00ff00) | pr | pb;
final int flippedIndex = (height - 1 - row) * width + col;
abgr[flippedIndex] = pix1;
}
}
private void runIncrementComparison(int width, int height, int[] argb)
{
final int[] abgr = new int[width * height];
int row = 0, col = 0;
for (int i = 0; i < argb.length; i++)
{
final int pix = argb[i];
final int pb = (pix >> 16) & 0xff;
final int pr = (pix << 16) & 0x00ff0000;
final int pix1 = (pix & 0xff00ff00) | pr | pb;
final int flippedIndex = (height - 1 - row) * width + col;
abgr[flippedIndex] = pix1;
if(++col >= width) {
++row;
col = 0;
}
}
}
}
@westnordost
Copy link
Author

On my phone, the output is

For random bitmaps of 1024x1024 pixels, the function takes:
With modulo/division code: 263ms
With increment/comparison code: 216ms
Thus, the new code is 17% faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment