C++ (Qt)QStringList lst;...for (int i = 0; i < lst.size(); ++i) lst.swap(qrand() % lst.size(), qrand() % lst.size());
C++ (Qt)QStringList list;...std::random_shuffle(list.begin(), list.end());
C++ (Qt)#include <iostream>#include <list>#include <algorithm>#include <QStringList>#include <ctime>#include <chrono> template <unsigned N>void initList(QStringList & list) { for (unsigned i = 0; i < N; ++i) { list << QString::number(i); }} int main(){ using namespace std::chrono; const unsigned Num = 1000000; QStringList list; initList<Num>(list); high_resolution_clock::time_point t1 = high_resolution_clock::now(); std::random_shuffle(list.begin(), list.end()); high_resolution_clock::time_point t2 = high_resolution_clock::now(); duration<double> time_span = duration_cast<duration<double>>(t2 - t1); std::cout << "random_shuffle: " << time_span.count() << " seconds." << std::endl; t1 = high_resolution_clock::now(); for (int i = 0; i < list.size(); ++i) { list.swap(qrand() % list.size(), qrand() % list.size()); } t2 = high_resolution_clock::now(); time_span = duration_cast<duration<double>>(t2 - t1); std::cout << "igors: " << time_span.count() << " seconds." << std::endl; return 0;}
Bashrandom_shuffle: 0.172468 seconds.igors: 0.403365 seconds. random_shuffle: 0.1679 seconds.igors: 0.350107 seconds. random_shuffle: 0.162135 seconds.igors: 0.362432 seconds.
C++ (Qt)#include <iostream>#include <algorithm>#include <QtGUI> template <unsigned N>void initList(QStringList & list) { for (unsigned i = 0; i < N; ++i) { list << QString::number(i); }} int main(){ const unsigned Num = 1000000; QStringList list; initList<Num>(list); QTime t1 = QTime::currentTime(); std::random_shuffle(list.begin(), list.end()); std::cout << "random_shuffle: " << t1.elapsed() << " ms." << std::endl; t1 = QTime::currentTime(); for (int i = 0; i < list.size(); ++i) list.swap(i, qrand() % list.size()); std::cout << "igors: " << t1.elapsed() << " ms." << std::endl; return 0;}
Bashrandom_shuffle: 163 ms.igors: 229 ms. random_shuffle: 159 ms.igors: 229 ms. random_shuffle: 192 ms.igors: 233 ms.
C++ (Qt)#include <iostream>#include <algorithm>#include <QtGUI> template <unsigned N>void initList(QStringList & list) { for (unsigned i = 0; i < N; ++i) { list << QString::number(i); }} int main(){ const unsigned Num = 1000000; QStringList list; initList<Num>(list); qsrand(0); QTime t2 = QTime::currentTime(); for (int i = 0; i < list.size(); ++i) list.swap(i, qrand() % list.size()); std::cout << "igors: " << t2.elapsed() << " ms." << std::endl; srand(0); QTime t1 = QTime::currentTime(); std::random_shuffle(list.begin(), list.end()); std::cout << "random_shuffle: " << t1.elapsed() << " ms." << std::endl; return 0;}
igors: 83 ms.random_shuffle: 59 ms.
C++ (Qt)QStringList lst;
C++ (Qt)for (int i = 0; i < lst.size(); ++i) lst.swap(qrand() % lst.size(), qrand() % lst.size());