Commit 907675e0 authored by CHAMONT David's avatar CHAMONT David
Browse files

Better exercice !

parent 40d2cd71
...@@ -1006,12 +1006,15 @@ ...@@ -1006,12 +1006,15 @@
"source": [ "source": [
"# Exercice\n", "# Exercice\n",
"\n", "\n",
"Rather than looking at each isolated code line, it is often profitable to take a look at the big picture : how to simply move one part of the computation, so to get exactly the same output, a lot faster ?" "Let's assume that we always run the program below with a `degree`which is even.\n",
"1. Replace `val *= data` with `val *= data*data`, and `j<degree` with `j<(degree/2)`. What is the effect on the execution time ?\n",
"2. Ask for `std::cout.precision(18)`, and compare the original and the previous implementation. Why the result is slightly differing ?\n",
"3. Try to compile with `-Ofast`."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 521, "execution_count": 536,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "skip" "slide_type": "skip"
...@@ -1029,50 +1032,50 @@ ...@@ -1029,50 +1032,50 @@
"source": [ "source": [
"%%file tmp.operations.cpp\n", "%%file tmp.operations.cpp\n",
"\n", "\n",
"#include <valarray>\n",
"#include <cstdlib>\n", "#include <cstdlib>\n",
"#include <array>\n", "#include <cassert>\n",
"\n", "#include <iostream>\n",
"int const SIZE = 1024 ;\n",
"int const REPEAT = 100000 ;\n",
"\n",
"using Array = std::array<double,SIZE> ;\n",
"\n", "\n",
"void randomize( Array & x ) {\n", "std::valarray<double> generate( int size )\n",
" srand(1) ;\n", " {\n",
" for ( int i=0 ; i<SIZE ; ++i )\n", " std::valarray<double> datas(size) ;\n",
" x[i] = (std::rand()/(RAND_MAX+1.)-0.5) ;\n", " for ( double & data : datas )\n",
"}\n", " { data = std::rand()/(RAND_MAX+1.) ; }\n",
" return datas ;\n",
" }\n",
"\n", "\n",
"void multiply( Array const & x, Array & y ) {\n", "double pow_reduce( std::valarray<double> const & datas, int degree )\n",
" for ( int r=0 ; r<REPEAT ; ++r )\n", " {\n",
" for ( int i=0 ; i<SIZE ; ++i )\n", " double res = 0 ;\n",
" y[i] += x[i]/10. ;\n", " for ( double data : datas )\n",
"}\n", " {\n",
" \n", " double val = 1 ; \n",
"void reduce( Array const & y ) {\n", " for ( int j=0 ; j<degree ; ++j )\n",
" double res {0.} ;\n", " val *= data ;\n",
" for ( int i=0 ; i<SIZE ; ++i ) {\n", " res += val ;\n",
" res += y[i] ;\n", " }\n",
" }\n", " return res ;\n",
" std::cout<<(res/SIZE)<<std::endl ;\n", " }\n",
"}\n",
"\n", "\n",
"int main()\n", "int main( int argc, char * argv[] )\n",
" {\n", " {\n",
" Array x, y ;\n", " assert(argc==3) ;\n",
" randomize(x) ;\n", " int size {atoi(argv[1])} ;\n",
" y.fill(0.) ;\n", " int degree {atoi(argv[2])} ;\n",
" multiply(x,y) ;\n", "\n",
" reduce(y) ;\n", " auto datas = generate(size) ;\n",
" auto res = pow_reduce(datas,degree) ;\n",
" std::cout << res << std::endl ;\n",
" }" " }"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 522, "execution_count": 545,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "subslide" "slide_type": "skip"
} }
}, },
"outputs": [ "outputs": [
...@@ -1088,24 +1091,16 @@ ...@@ -1088,24 +1091,16 @@
"%%file tmp.operations.sh\n", "%%file tmp.operations.sh\n",
"\n", "\n",
"rm -f tmp.operations.exe\n", "rm -f tmp.operations.exe\n",
"g++ -std=c++17 -O1 tmp.operations.cpp -o tmp.operations.exe\n", "g++ -std=c++17 -O3 tmp.operations.cpp -o tmp.operations.exe\n",
"./tmp.operations.exe $*\n", "time ./tmp.operations.exe $*"
"\n",
"rm -f tmp.operations.py\n",
"echo \"t = 0\" >> tmp.operations.py\n",
"for i in 0 1 2 3 4 5 6 7 8 9\n",
"do \\time -f \"t += %U\" -a -o ./tmp.operations.py ./tmp.operations.exe $* >> /dev/null\n",
"done\n",
"echo \"print('(~ {:.3f} s)'.format(t/10.))\" >> tmp.operations.py\n",
"python3 tmp.operations.py"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 523, "execution_count": 546,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "subslide" "slide_type": "skip"
} }
}, },
"outputs": [ "outputs": [
...@@ -1113,13 +1108,16 @@ ...@@ -1113,13 +1108,16 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"67.5054\n", "0.656155\r\n",
"(~ 0.142 s)\n" "\r\n",
"real\t0m1.767s\r\n",
"user\t0m1.767s\r\n",
"sys\t0m0.000s\r\n"
] ]
} }
], ],
"source": [ "source": [
"!bash -l ./tmp.operations.sh" "!bash -l ./tmp.operations.sh 1024 65536"
] ]
}, },
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment