How can I make this piece of code even faster?

Chris Angelico rosuav at gmail.com
Sat Jul 20 18:55:07 EDT 2013


On Sun, Jul 21, 2013 at 6:22 AM,  <pablobarhamalzas at gmail.com> wrote:
>             temp = 0
>             for y in range(input_num):
>                 count += 1
>                 temp += inputs[y] * h_weight[count]
>             hidden[x] = 1/(1+e**(-temp))

It's a micro-optimization that'll probably have negligible effect, but
it can't hurt: Instead of adding to temp and raising e to -temp, carry
the value of temp as a negative number:

                temp -= inputs[y] * h_weight[count]
            hidden[x] = 1/(1+e**temp)

Ditto in the second loop.

Not sure which way performance would go, but would it be more readable
to take an iterator for h_weight and o_weight? Something like this:

# Slot this into your existing structure
        inputs = self.input
        h_weight = iter(self.h_weight)
        o_weight = iter(self.o_weight)

        e = math.e

        for x in range(hidden_num):
            temp = 0
            for y in inputs:
                temp += y * next(h_weight)
            hidden[x] = 1/(1+e**(-temp))

        for x in range(output_num):
            temp = 0
            for y in hidden:
                temp += y * next(o_weight)
            output[x] = 1/(1+e**(-temp))
# End.

If that looks better, the next change I'd look to make is replacing
the 'for y' loops with sum() calls on generators:

temp = sum(y * next(o_weight) for y in hidden)

And finally replace the entire 'for x' loops with list comps... which
makes for two sizeable one-liners, which I like and many people
detest:

    def tick(self):
        inputs = self.inputs
        h_weight = iter(self.h_weight)
        o_weight = iter(self.o_weight)
        e = math.e
        hidden = [1/(1+e**sum(-y * next(h_weight) for y in inputs))
for _ in range(hidden_num)]
        self.output = [1/(1+e**sum(-y * next(o_weight) for y in
hidden)) for _ in range(output_num)]

Up to you to decide whether you find that version more readable, or at
least sufficiently readable, and then to test performance :) But it's
shorter by quite a margin, which I personally like. Oh, and I'm
relying on you to make sure I've made the translation correctly, which
I can't confirm without a pile of input data to test it on. All I can
say is that it's syntactically correct.

ChrisA



More information about the Python-list mailing list