-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_words.cpp
90 lines (76 loc) · 2.33 KB
/
count_words.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <omp.h>
#include <stdio.h>
#include <time.h>
using namespace std;
void fun_count_words(){
ifstream infile;
infile.open ("words1.txt");
ofstream outfile;
outfile.open ("output.txt");
map<string,int> dic;
pair<map<string,int>::iterator,bool> tmp;
map<string,int>::iterator it = dic.begin();
string word;
//double wtime = omp_get_wtime();
clock_t begin = clock();
if (infile.is_open()) {
//bool flag=true;
#pragma omp parrallel for num_threads(16) default (none)\
private (infile,word)
for (;infile>>word; ){
//while (file >> word){
tmp=dic.insert(pair<string,int>(word,1));
#pragma omp critical
if (tmp.second==false){
tmp.first->second+=1;
}
}
}else {
cout << "Error al abrir archivo";
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout<<"time: "<<elapsed_secs<<endl;
for (it=dic.begin(); it!=dic.end(); ++it)
cout << it->first << " " << it->second << '\n';
outfile.close();
}
int main () {
fun_count_words();
// ifstream infile;
// infile.open ("input_text.txt");
// ofstream outfile;
// outfile.open ("output.txt");
// map<string,int> dic;
// pair<map<string,int>::iterator,bool> ret;
// map<string,int>::iterator it = dic.begin();
// string word;
// //double wtime = omp_get_wtime();
// clock_t begin = clock();
// if (infile.is_open()) {
// //bool flag=true;
// #pragma omp parrallel for num_threads(16) default (none)\
// private (file,word)
// for (;infile>>word; ){
// //while (file >> word){
// ret=dic.insert(pair<string,int>(word,1));
// #pragma omp critical
// if (ret.second==false){
// ret.first->second+=1;
// }
// }
// }else {
// cout << "Error al abrir archivo";
// }
// clock_t end = clock();
// double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
// cout<<"time: "<<elapsed_secs<<endl;
// for (it=dic.begin(); it!=dic.end(); ++it)
// cout << it->first << " " << it->second << '\n';
// outfile.close();
// return 0;
}