Skip to content

Commit 14cb23e

Browse files
committed
updated benchmarks
1 parent 4b4f443 commit 14cb23e

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

fuzz-tests/numerical/Polynomial.cpp

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ struct Mod {
385385
Mod r = *this ^ (e / 2); r = r * r;
386386
return e&1 ? *this * r : r;
387387
}
388-
operator ll() { return x; }
388+
explicit operator ll() { return x; }
389389
};
390390

391391
typedef Mod num;
@@ -583,9 +583,10 @@ poly exp(poly a) {
583583
return modK(b, sz(a));
584584
}
585585
poly pow(poly a, ll m) {
586-
int p = 0; int n = sz(a);
586+
int p = 0, n = sz(a);
587587
while (p < sz(a) && a[p].x == 0)
588588
++p;
589+
if (ll(m)*p >= sz(a)) return poly(sz(a));
589590
num j = a[p];
590591
a = {a.begin() + p, a.end()};
591592
a = a * (num(1) / j);
@@ -612,14 +613,13 @@ vector<num> eval(const poly &a, const vector<num> &x) {
612613

613614
poly interp(vector<num> x, vector<num> y) {
614615
int n=sz(x);
615-
assert(n);
616616
vector<poly> up(n*2);
617617
rep(i,0,n) up[i+n] = poly({num(0)-x[i], num(1)});
618-
per(i,1,n) up[i] = up[2*i]*up[2*i+1];
618+
for(int i=n-1; i>0;i--) up[i] = up[2*i]*up[2*i+1];
619619
vector<num> a = eval(deriv(up[1]), x);
620620
vector<poly> down(2*n);
621621
rep(i,0,n) down[i+n] = poly({y[i]*(num(1)/a[i])});
622-
per(i,1,n) down[i] = down[i*2] * up[i*2+1] + down[i*2+1] * up[i*2];
622+
for(int i=n-1;i>0;i--) down[i] = down[i*2] * up[i*2+1] + down[i*2+1] * up[i*2];
623623
return down[1];
624624
}
625625

@@ -729,10 +729,10 @@ template <class A, class B> void testUnary(string name, A f1, B f2, int mxSz = 5
729729
}
730730
cout<<endl;
731731
}
732-
template <class A, class B> void testPow(string name, A f1, B f2, int mxSz = 5) {
732+
template <class A, class B> void testPow(string name, A f1, B f2, int mxSz = 5, int mxPref=5) {
733733
for (int it = 0; it < NUMITERS; it++) {
734734
auto a = genVec((rand() % mxSz) + 1);
735-
int pref = rand()%5;
735+
int pref = rand()%mxSz;
736736
for (int j=0; j<pref; j++) {
737737
a.first.insert(a.first.begin(), mine::num(0));
738738
a.second.insert(a.second.begin(), MIT::num(0));
@@ -745,6 +745,29 @@ template <class A, class B> void testPow(string name, A f1, B f2, int mxSz = 5)
745745
assert(checkEqual(res, t));
746746
}
747747
cout << name + " tests passed!" << endl;
748+
{
749+
timeit x("mine");
750+
for (int it = 0; it < NUMITERS; it++) {
751+
auto a = genVec((rand() % mxSz) + 1);
752+
int pref = rand()%mxPref;
753+
for (int j=0; j<pref; j++)
754+
a.first.insert(a.first.begin(), mine::num(0));
755+
int p = rand() % mxSz;
756+
f1(a.first, p);
757+
}
758+
}
759+
{
760+
timeit x("mit");
761+
for (int it = 0; it < NUMITERS; it++) {
762+
auto a = genVec((rand() % mxSz) + 1);
763+
int pref = rand()%mxPref;
764+
for (int j=0; j<pref; j++)
765+
a.second.insert(a.second.begin(), MIT::num(0));
766+
int p = rand() % mxSz;
767+
f2(a.second, p);
768+
}
769+
}
770+
cout<<endl;
748771
}
749772
template <class A, class B> void testEval(string name, A f1, B f2, int mxSz = 5) {
750773
for (int it = 0; it < NUMITERS; it++) {
@@ -757,6 +780,23 @@ template <class A, class B> void testEval(string name, A f1, B f2, int mxSz = 5)
757780
assert(checkEqual(res, t));
758781
}
759782
cout << name + " tests passed!" << endl;
783+
{
784+
timeit x("mine");
785+
for (int it = 0; it < NUMITERS; it++) {
786+
auto a = genVec((rand() % mxSz) + 1);
787+
auto b = genVec((rand() % mxSz)+1);
788+
f1(a.first, b.first);
789+
}
790+
}
791+
{
792+
timeit x("MIT");
793+
for (int it = 0; it < NUMITERS; it++) {
794+
auto a = genVec((rand() % mxSz) + 1);
795+
auto b = genVec((rand() % mxSz)+1);
796+
f2(a.second, b.second);
797+
}
798+
}
799+
cout<<endl;
760800
}
761801
template <class A, class B> void testInterp(string name, A f1, B f2, int mxSz = 5) {
762802
for (int it = 0; it < NUMITERS; it++) {
@@ -770,6 +810,23 @@ template <class A, class B> void testInterp(string name, A f1, B f2, int mxSz =
770810
assert(checkEqual(res, t));
771811
}
772812
cout << name + " tests passed!" << endl;
813+
{
814+
timeit x("mine");
815+
for (int it = 0; it < NUMITERS; it++) {
816+
auto a = genVec((rand() % mxSz) + 1);
817+
auto b = genVec((rand() % mxSz)+1);
818+
f1(a.first, b.first);
819+
}
820+
}
821+
{
822+
timeit x("MIT");
823+
for (int it = 0; it < NUMITERS; it++) {
824+
auto a = genVec((rand() % mxSz) + 1);
825+
auto b = genVec((rand() % mxSz)+1);
826+
f2(a.second, b.second);
827+
}
828+
}
829+
cout<<endl;
773830
}
774831
signed main() {
775832
ios::sync_with_stdio(0);
@@ -785,7 +842,7 @@ signed main() {
785842
testUnary("log", mine::log, MIT::log, SZ);
786843
testUnary("exp", mine::exp, MIT::exp, SZ);
787844
SZ = 1000;
788-
testPow("pow", mine::pow, MIT::pow, SZ);
845+
testPow("pow", mine::pow, MIT::pow, SZ, 5);
789846
testEval("eval", mine::eval, MIT::eval, SZ);
790847
testInterp("interp", mine::interp, MIT::interp, SZ);
791848
return 0;

0 commit comments

Comments
 (0)