* mul::expand(): Considerable speedup through caching in nested loop.
[ginac.git] / ginac / mul.cpp
index 81d648a599bd108319a225a959dea2accfca5899..c7fb32a1527123a02b9fd82ffc677672af49c870 100644 (file)
@@ -693,9 +693,10 @@ ex mul::expand(unsigned options) const
                                exvector distrseq;
                                distrseq.reserve(n1*n2);
                                for (int i1=0; i1<n1; ++i1) {
-                                       for (int i2=0; i2<n2; ++i2) {
-                                               distrseq.push_back(add1.op(i1) * add2.op(i2));
-                                       }
+                                       // cache the first operand (for efficiency):
+                                       const ex op1 = add1.op(i1);
+                                       for (int i2=0; i2<n2; ++i2)
+                                               distrseq.push_back(op1 * add2.op(i2));
                                }
                                last_expanded = (new add(distrseq))->
                                                 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));