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| 分类: 默认分类 |字号 订阅 网上有很多文本分割软件都是按字节大小来分割的,主要用于小说类的文本分割,对于比较有规则的内容按行数进行分割非常不方便 ...
随机推荐
- 利用docker容器运行.net core webapi
利用docker容器运行.net core webapi :first-child { margin-top: 0 !important; } > :last-child { margin-bo ...
- node.js UDP NAT 穿透实现
源码:https://github.com/zhongchengyi/node-udp-trunnel-demo 1.原理 A登录Server, NAT A 分配端口11000,Server得到A的地 ...
- java中把某个字符串中的单引号替换成双引号
String regexp = "\'";String str = "'good'";System.out.println("替换前:" + ...
- shell脚本查找tcp过多ip地址封掉
#!/bin/bash #hc source /etc/profile iplist=`netstat -ntu | awk '{print $5}'| cut -d':' -f1| sort |un ...
- $Noip2011/Luogu1315$ 观光公交 贪心
$Luogu$ $Sol$ 觉得这题贪心要想很多事情,不适合我这种没脑子选手$ovo$.看题解还理解了很久. 最开始是这样想的:把所有的路段上的乘客按大小排个序用加速器就好了,这个想法被自己轻松$ha ...
- [推荐]icheck-bootstrap(漂亮的ckeckbox/radiobox)
适用于Twitter Bootstrap框架的纯CSS样式的复选框/单选框按钮的插件. GitHub:https://github.com/bantikyan/icheck-bootstrap 如果你 ...
- 【JavaWeb学习】过滤器Filter
一.简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静 ...
- Scala实践1
一.Scala安装和配置 1.1安装 Scala需要Java运行时库,安装Scala需要首先安装jdk. 然后在Scala官网下载 程序安装包 根据不同的操作系统选择不同的安装包,下载完成后,将安装包 ...
- Broken Necklace 坏掉的项链 USACO 模拟(易错)
1004: 1.1.4Broken Necklace 坏掉的项链 时间限制: 1 Sec 内存限制: 128 MB提交: 11 解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目 ...
- VMware Workstation CentOS7 Linux 学习之路(4)--守护服务(Supervisor)
目前存在三个问题 问题1:ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现ASP.NET Core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生 ...