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| 分类: 默认分类 |字号 订阅 网上有很多文本分割软件都是按字节大小来分割的,主要用于小说类的文本分割,对于比较有规则的内容按行数进行分割非常不方便 ...
随机推荐
- AndroidStudio插件大全
Android-Studio-Plugins 原文地址:https://github.com/itgoyo/Android-Studio-Plugins 欢迎star~~~ Android-Studi ...
- Appium Mac系统 自动测试环境搭建
一.python 环境准备 Mac 自带 Python 环境,一般为 2.7 版本. 1.查看当前系统默认的Python路径 which python ==> /usr/bin/python 2 ...
- 洛谷P1220 关路灯 题解 区间DP
题目链接:https://www.luogu.com.cn/problem/P1220 本题涉及算法:区间DP. 我们一开始要做一些初始化操作,令: \(p[i]\) 表示第i个路灯的位置: \(w[ ...
- springboot-实现文件下载
一 前言 本文实现的文件下载是使用Apache 的 commons-fileupload 实现:在之前的springboot系列文件中已经讲述过如何实现多文件上传:这篇文件实现的文件下载功能主要是能在 ...
- 原生js面向对象编程-选项卡(自动轮播)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Yolo V3损失函数占个坑
https://blog.csdn.net/weixin_43384257/article/details/100974776目前来看讲的最清楚的博客 https://zhuanlan.zhihu.c ...
- 阿里开源服务发现组件 Nacos快速入门
最近几年随着云计算和微服务不断的发展,各大云厂商也都看好了微服务解决方案这个市场,纷纷推出了自己针对微服务上云架构的解决方案,并且诞生了云原生,Cloud Native的概念. 云原生是一种专门针对云 ...
- Vue中使用js-xlsx导出Data数据到Excel
需要引入的第三方JS有:export.js.xlsx.extendscript.js.xlsx.full.min.js JS太大不贴出来,放一个可下载百度云连接:https://pan.baidu.c ...
- 三分钟带你入门GitHub
一,首先,我们来说一下什么是GitHub GitHub是一个基于git打造的开源社区 ,同时也是一个大型同性交友平台 ,作为一个专业的程序员,你非常有必要知道并使用GitHub:作为一个国际化社区,所 ...
- 高校表白app使用体验
在本次软件工程专业交流会中,有幸了解了很多学长学姐研究并且开发的软件,使我受益匪浅.其中最让我记忆犹新的还属一款名为高校表白app的软件.首先这款app的主要功能是为我们提供一个委婉的告白平台,我们可 ...