Skip to content

Instantly share code, notes, and snippets.

@kolodziej kolodziej/Makefile
Last active Aug 29, 2015

Embed
What would you like to do?
przyklad: przyklad.cpp
g++ przyklad.cpp -o run
g++ przyklad.cpp -o run_o1 -O1
g++ przyklad.cpp -o run_o2 -O2
g++ przyklad.cpp -o run_o3 -O3
suma: suma.cpp
g++ suma.cpp -o suma
g++ suma.cpp -o suma_o1 -O1
g++ suma.cpp -o suma_o2 -O2
g++ suma.cpp -o suma_o3 -O3
#include <iostream>
#include <iomanip>
#include <ctime>
double duration(int (*f)(int), int n, int rep)
{
std::clock_t tic = std::clock();
for (int i = 0; i < rep; ++i)
f(n);
std::clock_t tac = std::clock();
return static_cast<double>(tac - tic) / CLOCKS_PER_SEC;
}
int rekurencja(int n)
{
if (n > 0)
rekurencja(n-1);
return 1;
}
int rekurencja_ogonowa(int n)
{
if (n == 0)
return 1;
return rekurencja_ogonowa(n-1);
}
int iteracja(int n)
{
for (int i = n; i >= 0; --i)
{
if (n == 0)
return 1;
}
}
int main(int argc, char** argv)
{
int n = 50;
int rep = 100000000;
std::cout << "Uruchomiono: " << argv[0] << "\n";
std::cout << std::setw(10) << "zwykla:" << std::setw(10) << duration(rekurencja, n, rep) << "\n";
std::cout << std::setw(10) << "ogonowa:" << std::setw(10) << duration(rekurencja_ogonowa, n, rep) << "\n";
std::cout << std::setw(10) << "iteracja:" << std::setw(10) << duration(iteracja, n, rep) << "\n";
return 0;
}
#include <iostream>
#include <iomanip>
#include <ctime>
double duration(int (*f)(int), int n, int rep)
{
std::clock_t tic = std::clock();
for (int i = 0; i < rep; ++i)
f(n);
std::clock_t tac = std::clock();
return static_cast<double>(tac - tic) / CLOCKS_PER_SEC;
}
int iter(int n)
{
int sum = 0;
for (int i = 0; true; ++i)
{
sum += i;
if (i == n)
break;
}
return sum;
}
int rec(int n, int sum = 0)
{
if (n == 0)
return sum;
return rec(n-1, sum+n);
}
int call_rec(int n)
{
return rec(n);
}
int main(int argc, char** argv)
{
int n = 50;
int rep = 50000000;
std::cout << "Uruchomiono: " << argv[0] << "\n";
std::cout << std::setw(10) << "iteracja:" << std::setw(10) << duration(iter, n, rep) << "\n";
std::cout << std::setw(10) << "rekurencja:" << std::setw(10) << duration(call_rec, n, rep) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.