[Numpy-discussion] quick optimization

Pete Shinners pete at shinners.org
Mon Oct 2 12:57:55 CDT 2000

i've got a quick optimization for the arrayobject.c source.
it speeds my usage of numpy up by about 100%. i've tested with
other numpy apps and noticed a minimum of about 20% speed.

anyways, in "do_sliced_copy", change out the following block:

    if (src_nd == 0 && dest_nd == 0) {
        for(j=0; j<copies; j++) {
            memcpy(dest, src, elsize);
            dest += elsize;
        return 0;

with this slightly larger one:

    if (src_nd == 0 && dest_nd == 0) {
        switch(elsize) {
        case sizeof(char):
            memset(dest, *src, copies);
        case sizeof(short):
            for(j=copies; j; --j, dest += sizeof(short))
                *(short*)dest = *(short*)src;
        case sizeof(long):
            for(j=copies; j; --j, dest += sizeof(int))
                *(int*)dest = *(int*)src;
        case sizeof(double):
            for(j=copies; j; --j, dest += sizeof(double))
                *(double*)dest = *(double*)src;
            for(j=copies; j; --j, dest += elsize)
                memcpy(dest, src, elsize);
        return 0;

anyways, you can see it's no brilliant algorithm change,
but for me, getting a free 2X speedup is a big help. i'm
hoping something like this can get merged into the next

after walking through the numpy code, i was surprised
how almost every function falls back to do_sliced_copy
(guess that's why it's at the top of the source?). that
made it a quick target for making optimization changes.

More information about the Numpy-discussion mailing list