C++ (Qt)#include <QRegExp>#include <QStringList>#include <QFile>#include <QTextStream>#include <QTextCodec>#include <QTime>#include <QDebug> template<typename Container>void dump( const Container &c ){ QTextStream out( stdout ); out.setCodec( QTextCodec::codecForName( "UTF8" ) ); for( typename Container::ConstIterator it = c.constBegin(); it != c.constEnd(); ++it ) out << "(" << it.key() << ", " << it.value() << ")"; out << '\n';} int main( int argc, char *argv[] ){ QFile in( "test.txt" ); if( !in.open( QIODevice::ReadOnly | QIODevice::Text ) ) { qWarning( "Test file not found." ); return 1; } const int num_iter = 100; QString src = QString::fromUtf8( in.readAll() ); QTime tm; tm.start(); for( int i = 0; i < num_iter; ++i ) { QHash<QString, int> hash; int start = 0; int cur = 0; QRegExp rx( "[\\s\\.,:;-()]+" ); for(;;) { cur = rx.indexIn( src, cur ); QString word = src.mid( start, (cur != -1)? cur - start : -1 ); if( !word.isEmpty() ) hash[ word ]++; if( cur == -1 ) break; start = ++cur; }// dump( hash ); } qDebug() << tm.elapsed(); tm.start(); for( int i = 0; i < num_iter; ++i ) { QMap <QString, int> MyMap; QStringList List = src.split(QRegExp("[\\s\\.,:;-()]+"),QString::SkipEmptyParts); foreach (QString Item, List) MyMap[Item] = (MyMap.contains(Item)) ? MyMap[Item]+1 : 1;// dump( MyMap ); } qDebug() << tm.elapsed(); return 0;}
#include <QRegExp>#include <QStringList>#include <QFile>#include <QTextStream>#include <QTextCodec>#include <QTime>#include <QDebug>template<typename Container>void dump( const Container &c ){ QTextStream out( stdout ); out.setCodec( QTextCodec::codecForName( "UTF8" ) ); for( typename Container::ConstIterator it = c.constBegin(); it != c.constEnd(); ++it ) out << "(" << it.key() << ", " << it.value() << ")"; out << '\n';}int main( int argc, char *argv[] ){ QFile in( "C:/Temp/test.txt" ); if( !in.open( QIODevice::ReadOnly | QIODevice::Text ) ) { qWarning( "Test file not found." ); return 1; } const int num_iter = 100; QString src = QString::fromUtf8( in.readAll() ); QTime tm; tm.start(); for( int i = 0; i < num_iter; ++i ) { QHash<QString, int> hash; int start = 0; int cur = 0; QRegExp rx( "[\\s\\.,:;\\-()]+" ); for(;;) { cur = rx.indexIn( src, cur ); QString word = src.mid( start, (cur != -1)? cur - start : -1 ); if( !word.isEmpty() ) hash[ word ]++; if( cur == -1 ) break; start = ++cur; }// dump( hash ); } qDebug() << tm.elapsed(); tm.start(); for( int i = 0; i < num_iter; ++i ) { QHash <QString, int> MyMap; QStringList List = src.split(QRegExp("[\\s\\.,:;\\-()]+"),QString::SkipEmptyParts); foreach (QString Item, List) MyMap[Item] = (MyMap.contains(Item)) ? MyMap[Item]+1 : 1;// dump( MyMap ); } qDebug() << tm.elapsed(); return 0;}
foreach (QString Item, List) MyMap[Item] = (MyMap.contains(Item)) ? MyMap[Item]+1 : 1;
foreach (QString Item, List) MyMap[Item]++;
C++ (Qt)#include <QRegExp>#include <QStringList>#include <QFile>#include <QTime>#include <QDebug>#include <string>#include <boost/tokenizer.hpp>#include <unordered_map>#include <fstream>#include <sstream> int main(){ QFile in( "test.txt" ); if( !in.open( QIODevice::ReadOnly | QIODevice::Text ) ) { qWarning( "Test file not found." ); return 1; } const int num_iter = 100; QString src = QString::fromUtf8( in.readAll() ); QTime tm; unsigned total = 0; tm.start(); for( int i = 0; i < num_iter; ++i ) { QHash<QString, int> hash; int start = 0; int cur = 0; QRegExp rx( "[\\s\\.,:;\\-()]+" ); for(;;) { cur = rx.indexIn( src, cur ); QString word = src.mid( start, (cur != -1)? cur - start : -1 ); if( !word.isEmpty() ) hash[ word ]++; if( cur == -1 ) break; start = ++cur; } total = hash.size(); // dump( hash ); } qDebug() << "Old:\t" << tm.elapsed() << total; tm.start(); for( int i = 0; i < num_iter; ++i ) { QHash <QString, int> MyMap; QStringList List = src.split(QRegExp("[\\s\\.,:;\\-()]+"),QString::SkipEmptyParts); foreach (QString Item, List) MyMap[Item]++; total = MyMap.size(); // dump( MyMap ); } qDebug() << "Majestio:\t" << tm.elapsed() << total; std::ifstream in_("test.txt"); if (!in_.is_open()) { qWarning( "Test file not found." ); return 1; } std::ostringstream oss; oss << in_.rdbuf(); std::string source = oss.str(); tm.start(); for( int i = 0; i < num_iter; ++i ) { std::unordered_map<std::string, unsigned> hash; boost::tokenizer<boost::char_separator<char>> tok(source, boost::char_separator<char>(" ,.\r\t\n:;-()")); for (auto it = tok.begin(); it != tok.end(); ++it) ++hash[*it]; total = hash.size(); } qDebug() << "m_ax:\t" << tm.elapsed() << total; return 0;}
BashOld: 15629 15211 Majestio: 17590 15211 m_ax: 9028 15211
C++ (Qt)#ifndef DAТABLOCK_H#define DAТABLOCK_H #include <QString>struct CDataBlock { CDataBlock( void ) {}; void Init( const QString & src ); //... члены данных как обсуждали выше}; #endif // DAТABLOCK_H
C++ (Qt)void MainWindow::bufferButtonSave(){ QClipboard *clipboard = QApplication::clipboard(); mDataBlock.Init(clipboard->text()); textory.clear(); textory.enableAdd( true ); QMapIterator<QString, int> it(mDataBlock.mFreqMap); while(it.hasNext()) { it.next(); textory.addItem(Textory::Item(1, it.key() + "\t" + QString::number(it.value())); } QString total = QString::number(mDataBlock.mWords.size()); ui.textoryWidget->getTextoryLabel().setText("Frequency list (" + total + ")" );}
C++ (Qt)struct CData { ...private: QString mSource; QVector <QStringRef> mWord; // принципиально - храним слова QHash <QStringRef, int> mFrequency; // возможно строить по запросу};