Qt库中的mapreduce是否保留输入文件的顺序?
Does mapreduce from the Qt library preserve the sequence of the input files?
WordCount countWords(const QString &file)
{
QFile f(file);
f.open(QIODevice::ReadOnly);
QTextStream textStream(&f);
WordCount wordCount;
while (textStream.atEnd() == false)
foreach (QString word, textStream.readLine().split(" "))
wordCount[word] += 1;
return wordCount;
}
...
QStringList files = findFiles("../../", QStringList() << "*.cpp" << "*.h");
...
int mapReduceTime = 0;
{
QTime time;
time.start();
WordCount total = mappedReduced(files, countWords, reduce);
mapReduceTime = time.elapsed();
qDebug() << "MapReduce" << mapReduceTime;
}
假设我想跟踪我正在处理的文件,我可以在countWord函数中创建一个全局静态变量,并在每次开始运行时增加它,以知道我正在对文件1进行一些处理吗?还是不可能知道哪个文件将首先被处理?我问是因为mapreduce允许并行处理,但我不知道操作系统将如何调度线程。
不可能确切地知道处理将以什么顺序运行,因为每个映射操作通常会花费不同的时间。
一个简单的解决方案是不处理原始文件列表,而是处理(index, filename)对的列表。
您还应该跳过空字符串,并且至少处理无法打开给定文件进行读取的错误。boolExpr == false
的习惯写法为! boolExpr
。
typedef QPair<int, QString> FileEntry;
WordCount countWords(const FileEntry &entry)
{
WordCount wordCount;
QFile f(entry.second);
if (!f.open(QIODevice::ReadOnly)) return wordCount;
QTextStream ts(&f);
while (!ts())
foreach (QString word, ts().split(" ", QString::SkipEmptyParts))
wordCount[word] ++;
return wordCount;
}
QStringList f = findFiles("../../", QStringList() << "*.cpp" << "*.h");
QList<FileEntry> fileEntries;
fileEntries.reserve(f.size());
int i = 0;
foreach (QString file, f)
fileEntries << qMakePair(i++, file);
mappedReduced(fileEntries, countWords, reduce);
相关文章:
- C++文本文件输入
- C++ 中文件输入输出中的长长 v/s int
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 用空格填充 2D 数组(从文件输入)
- 流对象来表示文件输入,然后是标准输入?
- 用精神解析文件输入
- 从文件输入和输出
- 来自文本文件输入 c++ 的向量
- 如何通过键盘或分数文件输入分数
- 了解文件输入和输出,我可以写入文件,但无法从文件打印
- 词频程序 - 文件输入太大?
- 尝试从 csv 文件输入大量数据时出错
- 如何将文件从文件输入到C 中的矩阵中
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 这是读取文件输入的正确方法吗?
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- C 文件输入流循环问题
- 使用对输入整数的文件输入