[Numpy-discussion] quick optimization

Paul F. Dubois pauldubois at home.com
Mon Nov 27 17:04:34 CST 2000

This optimization will be in the next release. Thanks!

-----Original Message-----
From: numpy-discussion-admin at lists.sourceforge.net
[mailto:numpy-discussion-admin at lists.sourceforge.net]On Behalf Of Pete
Sent: Monday, October 02, 2000 10:58 AM
To: Numpy Discussion
Subject: [Numpy-discussion] quick optimization

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.

Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net

More information about the Numpy-discussion mailing list