की बहस AndyProwl और एलन समाधान की तुलना में विकसित किया गया है के रूप में चलाया जाएगा, मैं दोनों समाधानों का बेंचमार्क किया है, और नतीजा ... तर्कों की संख्या पर निर्भर करता है।
साथ संकलन:
g++-4.7 -std=c++11 -Wall -Wextra -O3 main.cpp -o main -D_FIRST
मानक AndyProwl समाधान और साथ संकलन:
g++-4.7 -std=c++11 -Wall -Wextra -O3 main.cpp -o main -D_SECOND
मानक एलन समाधान।
यहां 10 तर्कों के लिए बेंचमार्क का कार्यक्रम है।
#include <iostream>
#include <chrono>
// Function 1 : with &&
template <typename Type>
inline bool f1(const Type& arg)
{
return arg;
}
template <typename Type, typename... Types>
inline bool f1(const Type& arg, const Types&... args)
{
bool arg1 = f1(arg);
bool arg2 = f1(args...);
return arg1 && arg2;
}
// Function 2 : with &
template <typename Type>
inline bool f2(const Type& arg)
{
return arg;
}
template <typename Type, typename... Types>
inline bool f2(const Type& arg, const Types&... args)
{
return f2(arg) & f2(args...);
}
// Benchmark
int main(int argc, char* argv[])
{
// Variables
static const unsigned long long int primes[10] = {11, 13, 17, 19, 23, 29, 31, 37, 41, 43};
static const unsigned long long int nbenchs = 50;
static const unsigned long long int ntests = 10000000;
unsigned long long int sum = 0;
double result = 0;
double mean = 0;
std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now();
// Loop of benchmarks
for (unsigned long long int ibench = 0; ibench < nbenchs; ++ibench) {
// Initialization
t0 = std::chrono::high_resolution_clock::now();
sum = 0;
// Loop of tests
for (unsigned long long int itest = 1; itest <= ntests; ++itest) {
#ifdef _FIRST
sum += f1((itest+sum)%primes[0], (itest+sum)%primes[1], (itest+sum)%primes[2], (itest+sum)%primes[3], (itest+sum)%primes[4], (itest+sum)%primes[5], (itest+sum)%primes[6], (itest+sum)%primes[7], (itest+sum)%primes[8], (itest+sum)%primes[9]);
#endif
#ifdef _SECOND
sum += f2((itest+sum)%primes[0], (itest+sum)%primes[1], (itest+sum)%primes[2], (itest+sum)%primes[3], (itest+sum)%primes[4], (itest+sum)%primes[5], (itest+sum)%primes[6], (itest+sum)%primes[7], (itest+sum)%primes[8], (itest+sum)%primes[9]);
#endif
}
// Finalization
result = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now()-t0).count();
mean += result;
std::cout<<"time = "<<result<<" (sum = "<<sum<<")"<<std::endl;
}
// End
std::cout<<"mean time = "<<mean/nbenchs<<std::endl;
return 0;
}
तर्क की दी गई संख्या के साथ प्रत्येक समाधान के लिए 50 मानक के साथ, फैलाव बहुत छोटा है, और इन मील के पत्थरों से अधिक मतलब समय एक विश्वसनीय संकेतक है।
मेरा पहला बेंचमार्क उन तर्कों की "दाएं" संख्या के साथ रहा है जहां एलोन समाधान एंडीप्रोएल समाधान से तेज़ है।
अंतिम परिणाम यहां हैं:
तो AndyProwl समाधान आम तौर पर तेजी एलन एक से है। तो, अब मैं आपके उत्तर को प्रमाणित कर सकता हूं। लेकिन मुझे लगता है कि अंतर इतना छोटा है कि यह वास्तुकला/कंपाइलर निर्भर है।
तो:
- AndyProwl +1 अपने आम तौर पर तेजी समाधान
- एलन + 1 आपके constexpr के लिए तैयार समाधान
के लिए के लिए कैसे जगह '' && साथ 'और' के बारे में? – fredoverflow