saveBytes/loadBytes performance
in
Programming Questions
•
6 months ago
I'm using the following function to write an image's pixel array to a temp file and then read them back into the array.
Does it have to do with saveBytes and loadBytes or the rest of the function I've written? Is it possible to make it any faster?
- void setup() {
- img = loadImage("image.png");
- }
- void keyPressed() {
- if (key == 's') {
- int t = millis();
- saveRAW("x.dat");
- println(millis()-t);
- }
- if (key == 'd') {
- int t = millis();
- loadRAW("x.dat");
- println(millis()-t);
- }
- }
- void saveRAW(String filename) {
- byte[] bytes = new byte[img.pixels.length*3];
- int index = 0;
- for (int i = 0; i < bytes.length; i++) {
- int c = img.pixels[index];
- bytes[i++] = (byte)((c >> 16) & 0xff);
- bytes[i++] = (byte)((c >> 8) & 0xff);
- bytes[i] = (byte)(c & 0xff);
- index++;
- }
- saveBytes(filename, bytes);
- }
- void loadRAW(String filename) {
- byte[] bytes = loadBytes(filename);
- int index = 0;
- int count = bytes.length/3;
- for (int i = 0; i<count; i++) {
- img.pixels[i] =
- 0xFF000000 |
- (bytes[index++] & 0xff) << 16 |
- (bytes[index++] & 0xff) << 8 |
- (bytes[index++] & 0xff);
- }
- }
I'm getting ~6ms on saveRaw and ~36ms on loadRAW. Can someone please clarify why loadRAW is so much slower? I mean are the two functions not pretty similar?
Does it have to do with saveBytes and loadBytes or the rest of the function I've written? Is it possible to make it any faster?
1