kis_iterator_benchmark.cpp 7.96 KB
Newer Older
Sven Langkamp's avatar
Sven Langkamp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  Copyright (c) 2007 Boudewijn Rempt <boud@valdyas.org>
 *  Copyright (c) 2007 Sven Langkamp <sven.langkamp@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_iterator_benchmark.h"
#include <QApplication>

#include <qtest_kde.h>
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include <KoColorProfile.h>

28
#include "kis_random_accessor_ng.h"
Sven Langkamp's avatar
Sven Langkamp committed
29
#include "kis_random_sub_accessor.h"
30
#include <kis_iterator_ng.h>
31
#include <kis_repeat_iterators_pixel.h>
32
#include <kis_sequential_iterator.h>
Sven Langkamp's avatar
Sven Langkamp committed
33 34 35

#include "kis_paint_device.h"

36 37 38
#define TEST_WIDTH 3000
#define TEST_HEIGHT 3000

Sven Langkamp's avatar
Sven Langkamp committed
39 40 41 42 43
void KisIteratorBenchmark::rectIter(const KoColorSpace * colorSpace)
{

    KisPaintDevice dev(colorSpace);

44 45
    quint8 * bytes = new quint8[colorSpace->pixelSize() * 64*64];
    memset(bytes, 128, 64 * 64 * colorSpace->pixelSize());
Sven Langkamp's avatar
Sven Langkamp committed
46 47 48 49

    QTime t;
    t.start();

Boudewijn Rempt's avatar
Boudewijn Rempt committed
50
    for (int i = 0; i < 3; i++) {
51
        KisRectIteratorSP it = dev.createRectIteratorNG(QRect(0, 0, TEST_WIDTH, TEST_HEIGHT));
52
        do {
53
            memcpy(it->rawData(), bytes, colorSpace->pixelSize());
54
        } while (it->nextPixel());
Sven Langkamp's avatar
Sven Langkamp committed
55

Boudewijn Rempt's avatar
Boudewijn Rempt committed
56
        qDebug() << "RectIterator run " << i  << "took" << t.elapsed();
Sven Langkamp's avatar
Sven Langkamp committed
57 58 59
        t.restart();
    }

Boudewijn Rempt's avatar
Boudewijn Rempt committed
60
    for (int i = 0; i < 3; i++) {
61 62 63 64 65 66
        KisRectIteratorSP it = dev.createRectIteratorNG(QRect(0, 0, TEST_WIDTH, TEST_HEIGHT));
        int nConseqPixels;
        do {
            nConseqPixels = it->nConseqPixels();
            memcpy(it->rawData(), bytes, nConseqPixels * colorSpace->pixelSize());
        } while (it->nextPixels(nConseqPixels));
67

68
        qDebug() << "RectIterator run (with nConseqPixels)" << i  << "took" << t.elapsed();
69 70 71
        t.restart();
    }

Sven Langkamp's avatar
Sven Langkamp committed
72 73 74
    delete[] bytes;
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
void KisIteratorBenchmark::sequentialIter(const KoColorSpace * colorSpace)
{

    KisPaintDeviceSP dev = new KisPaintDevice(colorSpace);

    quint8 * bytes = new quint8[colorSpace->pixelSize() * 64*64];
    memset(bytes, 128, 64 * 64 * colorSpace->pixelSize());

    QTime t;
    t.start();

    for (int i = 0; i < 3; i++) {
        KisSequentialIterator it(dev, QRect(0, 0, TEST_WIDTH, TEST_HEIGHT));
        do {
            memcpy(it.rawData(), bytes, colorSpace->pixelSize());
        } while (it.nextPixel());

        qDebug() << "SequentialIterator run " << i  << "took" << t.elapsed();
        t.restart();
    }

    t.restart();

    for (int i = 0; i < 3; i++) {
        KisSequentialConstIterator it(dev, QRect(0, 0, TEST_WIDTH, TEST_HEIGHT));
        do {
            //memcpy(it.rawData(), bytes, colorSpace->pixelSize());
        } while (it.nextPixel());

        qDebug() << "SequentialConstIterator run " << i  << "took" << t.elapsed();
        t.restart();
    }



    delete[] bytes;
}

113 114 115 116
void KisIteratorBenchmark::hLineIterNG(const KoColorSpace * colorSpace)
{
    KisPaintDevice dev(colorSpace);

117 118
    quint8 * bytes = new quint8[colorSpace->pixelSize() * 128];
    memset(bytes, 128, 128 * colorSpace->pixelSize());
119 120 121 122 123 124 125 126

    QTime t;
    t.start();

    for (int i = 0; i < 3; i++) {
        KisHLineIteratorSP it = dev.createHLineIteratorNG(0, 0, TEST_WIDTH);
        for (int j = 0; j < TEST_HEIGHT; j++) {
            do {
127
                memcpy(it->rawData(), bytes, colorSpace->pixelSize());
128 129 130 131 132 133 134 135 136 137 138 139 140 141
            } while (it->nextPixel());
            it->nextRow();
        }

        qDebug() << "HLineIteratorNG run " << i  << "took" << t.elapsed();
        t.restart();
    }

    for (int i = 0; i < 3; i++) {
        KisHLineIteratorSP it = dev.createHLineIteratorNG(0, 0, TEST_WIDTH);
        for (int j = 0; j < TEST_HEIGHT; j++) {
            int pixels;
            do {
                pixels = it->nConseqPixels();
142
                memcpy(it->rawData(), bytes, pixels * colorSpace->pixelSize());
143 144 145 146
            } while (it->nextPixels(pixels));
            it->nextRow();
        }

147
        qDebug() << "HLineIteratorNG with nConseqHPixels run " << i  << "took" << t.elapsed();
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        t.restart();
    }

    KisHLineConstIteratorSP cit = dev.createHLineConstIteratorNG(0, 0, TEST_WIDTH);
    for (int i = 0; i < TEST_HEIGHT; i++) {
        do {
            //do stuff
        } while (cit->nextPixel());
        cit->nextRow();
    }

    qDebug() << "const HLineIteratorNG took" << t.elapsed();

    delete[] bytes;
}

void KisIteratorBenchmark::vLineIterNG(const KoColorSpace * colorSpace)
{

    KisPaintDevice dev(colorSpace);
168
    quint8 * bytes = new quint8[colorSpace->pixelSize()];
169 170 171 172 173 174 175 176 177
    memset(bytes, 128, colorSpace->pixelSize());

    QTime t;
    t.start();

    for (int i = 0; i < 3; i++) {
        KisVLineIteratorSP it = dev.createVLineIteratorNG(0, 0, TEST_HEIGHT);
        for (int j = 0; j < TEST_WIDTH; j++) {
            do {
178
                memcpy(it->rawData(), bytes, colorSpace->pixelSize());
179 180 181 182 183 184 185 186 187 188
            } while(it->nextPixel());
            it->nextColumn();
        }

        qDebug() << "VLineIteratorNG run " << i  << " took" << t.elapsed();
        t.restart();
    }

    KisVLineConstIteratorSP cit = dev.createVLineConstIteratorNG(0, 0, TEST_HEIGHT);
    for (int i = 0; i < TEST_WIDTH; i++) {
189
        do {} while(cit->nextPixel());
190 191 192 193 194 195 196 197
        cit->nextColumn();
    }
    qDebug() << "const VLineIteratorNG took" << t.elapsed();

    delete[] bytes;

}

Sven Langkamp's avatar
Sven Langkamp committed
198 199 200 201
void KisIteratorBenchmark::randomAccessor(const KoColorSpace * colorSpace)
{

    KisPaintDevice dev(colorSpace);
202 203
    quint8 * bytes = new quint8[colorSpace->pixelSize() * 128];
    memset(bytes, 128, 128 * colorSpace->pixelSize());
Sven Langkamp's avatar
Sven Langkamp committed
204 205 206 207

    QTime t;
    t.start();

Boudewijn Rempt's avatar
Boudewijn Rempt committed
208
    for (int i = 0; i < 3; i++) {
209
        KisRandomAccessorSP ac = dev.createRandomAccessorNG(0, 0);
210 211
        for (int y = 0; y < TEST_HEIGHT; ++y) {
            for (int x = 0; x < TEST_WIDTH; ++x) {
212
                ac->moveTo(x, y);
213
                memcpy(ac->rawData(), bytes, colorSpace->pixelSize());
Sven Langkamp's avatar
Sven Langkamp committed
214 215 216
            }
        }

Boudewijn Rempt's avatar
Boudewijn Rempt committed
217
        qDebug() << "RandomIterator run " << i  << " took" << t.elapsed();
Sven Langkamp's avatar
Sven Langkamp committed
218 219 220
        t.restart();
    }

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    for (int i = 0; i < 3; i++) {
        KisRandomAccessorSP ac = dev.createRandomAccessorNG(0, 0);
        for (int y = 0; y < TEST_HEIGHT; ) {
            int numContiguousRows = qMin(ac->numContiguousRows(y), TEST_HEIGHT - y);

            for (int x = 0; x < TEST_WIDTH; ) {
                int numContiguousColumns = qMin(ac->numContiguousColumns(x), TEST_WIDTH - x);

                ac->moveTo(x, y);
                int rowStride = ac->rowStride(x, y);
                quint8 *data = ac->rawData();


                for (int i = 0; i < numContiguousRows; i++) {
                    memcpy(data, bytes, numContiguousColumns * colorSpace->pixelSize());
                    data += rowStride;
                }

                x += numContiguousColumns;
            }
            y += numContiguousRows;
        }

        qDebug() << "RandomIterator run (with strides)" << i  << " took" << t.elapsed();
        t.restart();
    }

248
    KisRandomConstAccessorSP cac = dev.createRandomConstAccessorNG(0, 0);
249 250
    for (int y = 0; y < TEST_HEIGHT; ++y) {
        for (int x = 0; x < TEST_WIDTH; ++x) {
251
            cac->moveTo(x, y);
Sven Langkamp's avatar
Sven Langkamp committed
252 253 254
        }
    }

Boudewijn Rempt's avatar
Boudewijn Rempt committed
255
    qDebug() << "const RandomIterator took" << t.elapsed();
Sven Langkamp's avatar
Sven Langkamp committed
256 257 258 259 260 261 262 263 264

    delete[] bytes;
}


void KisIteratorBenchmark::runBenchmark()
{
    const KoColorSpace* cs = KoColorSpaceRegistry::instance()->rgb8();

265 266
    hLineIterNG(cs);
    vLineIterNG(cs);
Sven Langkamp's avatar
Sven Langkamp committed
267
    rectIter(cs);
268
    sequentialIter(cs);
Sven Langkamp's avatar
Sven Langkamp committed
269 270 271 272 273
    randomAccessor(cs);
}

QTEST_KDEMAIN(KisIteratorBenchmark, NoGUI)
#include "kis_iterator_benchmark.moc"