C++ 对TXT 的串并行读写
任务说明:有36篇文档,现在要读入,并统计词频,字典长度25,希望能够比较串并行读写操作的时间差距。
- 串行读入并统计词频
// LoadDocsInUbuntu.cpp // #include <iostream> #include <stdio.h> #include <vector> using namespace std; int main() { char filename[100]; size_t d; FILE *fileptr; int word; vector< vector<int> > corpus; printf("load data ...\n"); for (d = 1; d < 37; d++){ sprintf(filename, "..//data/doc_%d.txt", d); fileptr = fopen(filename, "r"); vector<int> doc; int ff[25] = { 0 }; while (fscanf(fileptr, "%d", &word) != EOF) { ff[word - 1] = ff[word - 1] + 1; doc.push_back(word); } corpus.push_back(doc); fclose(fileptr); sprintf(filename, "..//result/freqUbuntuSerial_%d.txt", d); fileptr = fopen(filename, "w"); for (int f = 0; f < 25; f++) { fprintf(fileptr, "%d ", ff[f]); } fclose(fileptr); } cout <<"corpus.size()="<< corpus.size() << endl; return 0; } - 这里讨论并行有三种思路:一,按照文档序号进行分组读入统计等操作;二,在文档内按单词数目分组进行统计;三,将统计与读写操作并行处理。
针对第一种思路,使用openmp做多线程处理:
// LoadDocsByOpenMP.cpp
//
#include <omp.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std; int main()
{
char filename[100],resultname[100];
int d;
FILE *fileptr[360];
int word;
int ff[360][25] = { 0 };
//vector< vector<int> > corpus;
clock_t start,finish;
int f[360]={0}; start=clock();
printf("load data ...\n");
#pragma omp parallel for num_threads(4)
for (d = 1; d < 361; d++){
printf("Hello world, I am %d, docs index %d.\n",omp_get_thread_num(),d);
sprintf(filename, "..//data/doc_%d.txt", d);
fileptr[d-1] = fopen(filename, "r");
//int ff[25]={0};
////vector<int> doc; while (fscanf(fileptr[d-1], "%d", &word) != EOF)
{
ff[d-1][word - 1] = ff[d-1][word - 1] + 1;
//ff[word-1]=ff[word-1]+1;
// //doc.push_back(word);
}
////corpus.push_back(doc);
fclose(fileptr[d-1]);
sprintf(resultname, "..//result/freqByOpenMP_%d.txt", d);//Be CAREFUL!For the name "filename" has been used before, we must name the string differently here.
fileptr[d-1] = fopen(resultname, "w");
for (f[d-1] = 0; f[d-1] < 25; f[d-1]++)
{ fprintf(fileptr[d-1], "%d ", ff[f[d-1]]);
}
fclose(fileptr[d-1]);
} //cout <<"corpus.size()="<< corpus.size() << endl;
finish=clock();
cout<<"time cost : "<< (double)(finish-start)/ CLOCKS_PER_SEC<<endl;
return 0;
}
但初步比较openmp对串行读取的速度并没有太多提升,反而是当进程数多于系统物理核数的时候,程序时间会加长。
另外两种实现思路在后续学习中继续实现。
C++ 对TXT 的串并行读写的更多相关文章
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- Java初学者笔记四:按行读写文件和输入处理
一.我们来看python的很简单: 1.读文件: with open("/path/file","r") as fr: for line in fr.readl ...
- txt文件按行处理工具类(可以截取小说、分析日志等)【我】
txt文件按行处理工具类(可以分析日志.截取小说等) package file; import java.io.BufferedReader; import java.io.BufferedWrite ...
- iOS 9应用开发教程之多行读写文本ios9文本视图
iOS 9应用开发教程之多行读写文本ios9文本视图 多行读写文本——ios9文本视图 文本视图也是输入控件,与文本框不同的是,文本视图可以让用户输入多行,如图2.23所示.在此图中字符串“说点什么吧 ...
- 63.当当网txt数据按行切割与合并
获取文件有多少行 //获取文件有多少行 int getN(char *path) { FILE *pf = fopen(path, "r"); if (pf==NULL) { ; ...
- cv.Mat 与 .txt 文件数据的读写操作
1.按OpenCV格式实现的 .txt 文件读写 可以用 cvSave 和 cvLoad 实现,格式和 .xml/.yml 的差不多,不过如果专用与 OpenCV 的数据读写,还是用 .xml/.y ...
- 获取txt文件指定行内容
#!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1; #表示行 ...
- Linux平台下利用系统接口函数按照行读写文件
要求:支持大文件(1M)一次性读入 源代码如下: #include<stdio.h> #include<fcntl.h> #include<stdlib.h> #i ...
- 如何把一个TXT文本文件按行数分割成多个文本文件
2011-04-27 12:00:24| 分类: 默认分类 |字号 订阅 网上有很多文本分割软件都是按字节大小来分割的,主要用于小说类的文本分割,对于比较有规则的内容按行数进行分割非常不方便 ...
随机推荐
- 解析crash
命令行 1.查找 symbolicatecrash find /Applications/Xcode.app -name symbolicatecrash -type f 2.此时会出现一个路径 sy ...
- Kafka学习笔记(四)—— API使用
Kafka学习笔记(四)-- API使用 1.Producer API 1.1 消息发送流程 Kafka的Producer发送消息采用的是异步发送的方式.在消息发送的过程中,涉及到了两个线程--mai ...
- $Loj10157$ 皇宫看守 树形$DP$
loj Description 有一些宫殿,它们呈树形结构,相邻的宫殿之间可以互相望见.在一些宫殿设立士兵,使得所有的宫殿都有士兵或是被士兵望见.求最小士兵数. Sol 状态: f[x][0] 表示结 ...
- win10纯净版安装及其常用软件集锦(2020新年湘岳阳万江波整理)
win10纯净版安装及其常用软件集锦 1.安装win10纯净版:链接:https://pan.baidu.com/s/1L9yl-LNxxDQbEN_TGswzcA 提取码:u0pt 2.安装WPS2 ...
- springboot-实现文件下载
一 前言 本文实现的文件下载是使用Apache 的 commons-fileupload 实现:在之前的springboot系列文件中已经讲述过如何实现多文件上传:这篇文件实现的文件下载功能主要是能在 ...
- .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法
行为不一致 .NET Core 3.0 新出了个内置的 JSON 库, 全名叫做尼古拉斯 System.Text.Json - 性能更高占用内存更少这都不是事... 对我来说, 很多或大或小的项目能少 ...
- 《图解机器学习-杉山将著》读书笔记---CH3
CH3 最小二乘学习法 重点提炼 提出最小二乘学习法的缘故: 最小二乘学习法公式 对不同模型进行最小二乘法学习,得到最小二乘公式中的参数theta: 1.线性模型 代入3.1公式,对参数求偏导,偏 ...
- MD5:js,java,C#三种语言加密结果不同解决办法
最近遇到前端js MD5加密与后端C#与Java MD5加密结果不一致的问题,所以写个关于此问题的解决办法 前端js引用的md5类库,类库地址:https://blueimp.github.io/Ja ...
- async-await 线程分析
这里没有线程 原文地址:https://blog.stephencleary.com/2013/11/there-is-no-thread.html 前言 我是在看 C#8.0 新特性异步流时在评论里 ...
- cf - 920 c 求能否实现交换
C. Swap Adjacent Elements time limit per test 1 second memory limit per test 256 megabytes input sta ...