From 32f16fdb64f61370d708124af960adef0c95b095 Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 16 Jul 2020 12:48:20 +0300 Subject: [PATCH] optimize piSwap and c++11 improvments --- lib/main/containers/pideque.h | 2 + lib/main/containers/pimap.h | 10 +- lib/main/containers/pivector.h | 2 + lib/main/core/pibase.h | 12 +- lib/main/core/pibytearray.h | 1 + lib/main/core/piobject.h | 10 +- lib/main/core/pistring.h | 1 + main.cpp | 201 ++++++++++++++++++++++++++++++--- 8 files changed, 216 insertions(+), 23 deletions(-) diff --git a/lib/main/containers/pideque.h b/lib/main/containers/pideque.h index ff3ba8f8..f9c24a15 100644 --- a/lib/main/containers/pideque.h +++ b/lib/main/containers/pideque.h @@ -538,5 +538,7 @@ inline PICout operator <<(PICout s, const PIDeque & v) { return s; } +template inline void piSwap(PIDeque & f, PIDeque & s) {f.swap(s);} + #endif // PIDEQUE_H diff --git a/lib/main/containers/pimap.h b/lib/main/containers/pimap.h index f176dab4..f397a09f 100644 --- a/lib/main/containers/pimap.h +++ b/lib/main/containers/pimap.h @@ -57,7 +57,7 @@ void piQuickSort(T * a, ssize_t N) { if (i <= j) { if (i != j) { //piCout << "swap" << i << j << a[i] << a[j]; - piSwapBinary(a[i], a[j]); + piSwap(a[i], a[j]); } i++; j--; } @@ -216,8 +216,8 @@ public: PIMap & clear() {pim_content.clear(); pim_index.clear(); return *this;} void swap(PIMap & other) { - piSwapBinary >(pim_content, other.pim_content); - piSwapBinary >(pim_index, other.pim_index); + pim_content.swap(other.pim_content); + pim_index.swap(other.pim_index); } PIMap & insert(const Key & key, const T & value) { @@ -292,7 +292,7 @@ protected: pim_index[i].index = ci; break; } - piSwapBinary(pim_content[ci], pim_content.back()); + piSwap(pim_content[ci], pim_content.back()); pim_content.resize(pim_index.size()); } const value_type _pair(ssize_t index) const { @@ -342,5 +342,7 @@ inline PICout operator <<(PICout s, const PIMap & v) { return s; } +template inline void piSwap(PIMap & f, PIMap & s) {f.swap(s);} + #endif // PIMAP_H diff --git a/lib/main/containers/pivector.h b/lib/main/containers/pivector.h index a389f05f..2f4ae66e 100644 --- a/lib/main/containers/pivector.h +++ b/lib/main/containers/pivector.h @@ -487,5 +487,7 @@ inline PICout operator <<(PICout s, const PIVector & v) { return s; } +template inline void piSwap(PIVector & f, PIVector & s) {f.swap(s);} + #endif // PIVECTOR_H diff --git a/lib/main/core/pibase.h b/lib/main/core/pibase.h index e55bb696..5f0f280d 100644 --- a/lib/main/core/pibase.h +++ b/lib/main/core/pibase.h @@ -255,9 +255,15 @@ typedef unsigned long long ullong; typedef long long llong; typedef long double ldouble; +#ifdef PIP_CXX11_SUPPORT +#define piMove(v) std::move(v) +#else +#define piMove(v) v +#endif + /*! \brief Templated function for swap two values * \details Example:\n \snippet piincludes.cpp swap */ -template inline void piSwap(T & f, T & s) {T t = f; f = s; s = t;} +template inline void piSwap(T & f, T & s) {T t(piMove(f)); f = piMove(s); s = piMove(t);} /*! \brief Templated function for swap two values without "=" * \details Example:\n \snippet piincludes.cpp swapBinary */ @@ -294,6 +300,10 @@ template<> inline void piSwapBinary(const void *& f, const void *& s) { } } +template<> inline void piSwap(double & f, double & s) {piSwapBinary(f, s);} +template<> inline void piSwap(ldouble & f, ldouble & s) {piSwapBinary(f, s);} + + /*! \brief Function for compare two values without "=" by raw content * \details Example:\n \snippet piincludes.cpp compareBinary */ inline bool piCompareBinary(const void * f, const void * s, size_t size) { diff --git a/lib/main/core/pibytearray.h b/lib/main/core/pibytearray.h index cee46ce1..47f61f78 100644 --- a/lib/main/core/pibytearray.h +++ b/lib/main/core/pibytearray.h @@ -316,6 +316,7 @@ __PIBYTEARRAY_SIMPLE_TYPE__(PIChar) template<> inline uint piHash(const PIByteArray & ba) {return ba.hash();} +template<> inline void piSwap(PIByteArray & f, PIByteArray & s) {f.swap(s);} #endif // PIBYTEARRAY_H diff --git a/lib/main/core/piobject.h b/lib/main/core/piobject.h index 59b6b6a6..8d34f644 100644 --- a/lib/main/core/piobject.h +++ b/lib/main/core/piobject.h @@ -504,13 +504,19 @@ public: explicit PIObject(const PIString & name = PIString()); virtual ~PIObject(); - + +#ifdef PIP_CXX11_SUPPORT + explicit PIObject(const PIObject & ) = delete; + void operator =(const PIObject & ) = delete; +#else private: explicit PIObject(const PIObject & ); void operator =(const PIObject & ); +#endif +private: uint _signature_; - + public: //! Returns object name diff --git a/lib/main/core/pistring.h b/lib/main/core/pistring.h index 5afe1173..e7affb17 100644 --- a/lib/main/core/pistring.h +++ b/lib/main/core/pistring.h @@ -770,5 +770,6 @@ PIString versionNormalize(const PIString & v); template<> inline uint piHash(const PIString & s) {return s.hash();} +template<> inline void piSwap(PIString & f, PIString & s) {f.swap(s);} #endif // PISTRING_H diff --git a/main.cpp b/main.cpp index bf4ec44a..c335b22a 100644 --- a/main.cpp +++ b/main.cpp @@ -110,27 +110,196 @@ private: }; */ +struct A { + double arr[1000]; +}; //PIKbdListener kbd(0, 0, false); +void swap(int & x, int & y) {int t = x; x = y; y = t;} +void swap2(int & x, int & y) {int t(std::move(x)); x = y; y = t;} +void swap(size_t & x, size_t & y) {size_t t = x; x = y; y = t;} +void swap2(size_t & x, size_t & y) {size_t t{std::move(x)}; x = y; y = t;} +void swap(double & x, double & y) {double t = x; x = y; y = t;} +void swap2(double & x, double & y) {double t(std::move(x)); x = std::move(y); y = std::move(t);} +void swap(float & x, float & y) {float t = x; x = y; y = t;} +void swap2(float & x, float & y) {float t(std::move(x)); x = std::move(y); y = std::move(t);} +void swap(PIString & x, PIString & y) {PIString t = x; x = y; y = t;} +void swap2(PIString & x, PIString & y) {PIString t(std::move(x)); x = std::move(y); y = std::move(t);} +void swap(std::string & x, std::string & y) {std::string t = x; x = y; y = t;} +void swap2(std::string & x, std::string & y) {std::string t{std::move(x)}; x = std::move(y); y = std::move(t);} +void swap(A & x, A & y) {A t = x; x = y; y = t;} +void swap2(A & x, A & y) {A t{std::move(x)}; x = std::move(y); y = std::move(t);} +void swap(PIObject & x, PIObject & y) {A t = x; x = y; y = t;} +//void swap2(PIObject & x, PIObject & y) {A t(std::move(x)); x = std::move(y); y = std::move(t);} -#include "pigpio.h" int main(int argc, char * argv[]) { - PIObject * o = PIIODevice::createFromFullPath("ser://COM1"); - piCout << o; - piCout << o->cast(); - piCout << o->cast(); - piCout << o->scopeList(); - piCout << "\n\n"; - PIMap & m(PIObject::__meta_data()); - for (auto it = m.constBegin(); it != m.constEnd(); ++it) { - const PIObject::__MetaData & md(it.value()); - piCout << it.key() << md.scope_list << md.scope_id << ":"; - for (auto j = md.eh_func.constBegin(); j != md.eh_func.constEnd(); ++j) { - piCout << " " << j.value().fullFormat(); - } - piCout << ""; - }/**/ + PITimeMeasurer tm; + int m = 100000; + int a = 99, b = 77; + piCout << "int"; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(a,b); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(a,b); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(a,b); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(a,b); + } + piCout << tm.elapsed_s(); + + piCout << "size_t"; + size_t ta = 99, tb = 77; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(ta,tb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(ta,tb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(ta,tb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(ta,tb); + } + piCout << tm.elapsed_s(); + + piCout << "ullong"; + ullong lla = 99, llb = 77; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(lla,llb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(lla,llb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(lla,llb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(lla,llb); + } + piCout << tm.elapsed_s(); + + piCout << "double"; + double da = 0.99,db = 77.77; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(da,db); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(da,db); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(da,db); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(da,db); + } + piCout << tm.elapsed_s(); + + piCout << "float"; + float fa = 0.99,fb = 77.77; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(fa,fb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(fa,fb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(fa,fb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(fa,fb); + } + piCout << tm.elapsed_s(); + + piCout << "A"; + A aa,ab; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(aa,ab); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(aa,ab); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(aa,ab); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(aa,ab); + } + piCout << tm.elapsed_s(); + + piCout << "std::string"; + std::string ia = "123456789012345678901vfsdvsd2345678",ib = "qwertyvsdfvvsdvfsuiopqwertyuikolsdfghjklsdfghjk"; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(ia,ib); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(ia,ib); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + ia.swap(ib); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(ia,ib); + } + piCout << tm.elapsed_s(); + + + PIString sa = "123456789012345678901vfsdvsd2345678",sb = "qwertyvsdfvvsdvfsuiopqwertyuikolsdfghjklsdfghjk"; + piCout << "PIString"; + tm.reset(); + for (int i = 0; i < m; ++i) { + swap(sa,sb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + swap2(sa, sb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + sa.swap(sb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwapBinary(sa, sb); + } + piCout << tm.elapsed_s(); tm.reset(); + for (int i = 0; i < m; ++i) { + piSwap(sa, sb); + } + piCout << tm.elapsed_s(); return 0; }