Commit b98ae015 authored by CHAMONT David's avatar CHAMONT David
Browse files

Review 4/1/categories Exercice

parent 4d4319b3
#include <iostream>
#include <chrono>
#include <type_traits>
#include <string>
// categories
struct TestTag {} ;
struct TestChronoTag {} ;
// tests
struct StringCopy
{
using category = TestChronoTag ;
char const * const title
{"Timing a std::string copy"} ;
void operator()() const
{
std::string s1 {"dummy"}, s2 ;
for ( int i = 0 ; i<1000 ; ++i )
{
if (i%2) s2 = s1 ;
else s1 = s2 ;
}
}
} ;
struct StringMove
{
using category = TestChronoTag ;
char const * const title
{"Timing a std::string move"} ;
void operator()() const
{
std::string s1 {"dummy"}, s2 ;
for ( int i = 0 ; i<1000 ; ++i )
{
if (i%2) s2 = std::move(s1) ;
else s1 = std::move(s2) ;
}
}
} ;
struct Accumulate
{
using category = TestTag ;
char const * const title
{"Test accumulating 0.1"} ;
void operator()() const
{
std::cout.precision(18) ;
double acc {} ;
for ( int i = 0 ; i<10 ; ++i )
{
acc += 0.1 ;
std::cout<<acc<<std::endl ;
}
}
} ;
// execution generic framework
template< typename TestT >
void execute_single_test()
{
TestT test ;
std::cout<<std::endl ;
std::cout<<"== " ;
std::cout<<(test.title)<<std::endl ;
if constexpr (std::is_same_v<typename TestT::category,TestChronoTag>)
{
using namespace std::chrono ;
auto t1 = steady_clock::now() ;
test() ;
auto t2 = steady_clock::now() ;
std::cout<<"Execution time: "
<<duration_cast<microseconds>(t2-t1).count()
<<" us."<<std::endl ;
}
else if constexpr (std::is_same_v<typename TestT::category,TestTag>)
{ test() ; }
}
template< typename... TestTs >
void execute()
{ ( execute_single_test<TestTs>() , ... ) ; }
// main program
int main()
{
execute<StringCopy,StringMove,Accumulate>() ;
}
#include <iostream>
#include <chrono>
#include <type_traits>
#include <string>
// external unmodifiable tests
struct Accumulate
{
char const * const title
{"Test accumulating 0.1"} ;
void operator()() const
{
std::cout.precision(18) ;
double acc {} ;
for ( int i = 0 ; i<10 ; ++i )
{
acc += 0.1 ;
std::cout<<acc<<std::endl ;
}
}
} ;
// your categories
struct TestTag {} ;
struct TestChronoTag {} ;
// your additional tests
struct StringCopy
{
using category = TestChronoTag ;
char const * const title
{"Timing a std::string copy"} ;
void operator()() const
{
std::string s1 {"dummy"}, s2 ;
for ( int i = 0 ; i<1000 ; ++i )
{
if (i%2) s2 = s1 ;
else s1 = s2 ;
}
}
} ;
struct StringMove
{
using category = TestChronoTag ;
char const * const title
{"Timing a std::string move"} ;
void operator()() const
{
std::string s1 {"dummy"}, s2 ;
for ( int i = 0 ; i<1000 ; ++i )
{
if (i%2) s2 = std::move(s1) ;
else s1 = std::move(s2) ;
}
}
} ;
// traits
template< typename TestT >
struct TestTraits
{ using category = typename TestT::category ; } ;
template<>
struct TestTraits<Accumulate>
{ using category = TestTag ; } ;
// execution generic framework
template< typename TestT >
void execute_single_test()
{
TestT test ;
std::cout<<std::endl ;
std::cout<<"== " ;
std::cout<<(test.title)<<std::endl ;
if constexpr (std::is_same_v<typename TestTraits<TestT>::category,TestChronoTag>)
{
using namespace std::chrono ;
auto t1 = steady_clock::now() ;
test() ;
auto t2 = steady_clock::now() ;
std::cout<<"Execution time: "
<<duration_cast<microseconds>(t2-t1).count()
<<" us."<<std::endl ;
}
else if constexpr (std::is_same_v<typename TestTraits<TestT>::category,TestTag>)
{ test() ; }
}
template< typename... TestTs >
void execute()
{ ( execute_single_test<TestTs>() , ... ) ; }
// main program
int main()
{
execute<StringCopy,StringMove,Accumulate>() ;
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Un apercu de C++20\n",
"\n",
"Le standard à venir fourmille de multiples micro-nouveautés, mais nous allons\n",
"nous focaliser sur les quatre nouveautés très importantes que Rainer Grimm\n",
"appelle \"The Big Four\", et qui vont nous faire basculer dans le C++\n",
"\"post-moderne\" :\n",
"\n",
"1. en matière de compilation : les [**modules**](fr.1-modules.ipynb) ;\n",
"1. côté programmation générique : les [**concepts**](fr.2-concepts.ipynb) ;\n",
"1. côté programmation fonctionnelle : les [**ranges**](fr.3-ranges.ipynb) ;\n",
"1. côté programmation asynchrone : les [**co-routines**](fr.4-coroutines.ipynb).\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"AVERTISSEMENT : toutes ces fonctionnalités sont plutôt nouvelles, ne vous attendez pas à des explications très avancées."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"---\n",
"## Sources\n",
"\n",
"* [Modernes Cpp](http://modernescpp.com/index.php/thebigfour)\n",
"* [Cpp Reference](https://en.cppreference.com/w/cpp/compiler_support)\n",
"* [Wikipedia](https://en.wikipedia.org/wiki/C%2B%2B20)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"© *CNRS 2020* \n",
"*Assembled and written by David Chamont, this work is made available according to the terms of the* \n",
"[*Creative Commons License - Attribution - NonCommercial - ShareAlike 4.0 International*](http://creativecommons.org/licenses/by-nc-sa/4.0/)"
]
}
],
"metadata": {
"celltoolbar": "Diaporama",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
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