/*************************************************************************
> File Name: 2.prime_count.cpp
> Author: huguang
> Mail: hug@haizeix.com
> Created Time:
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <thread>
#include <mutex>
using namespace std;
#define BEGIN(name) namespace name {
#define END(name) }
#define MAX_N 5000000
#define TIME(func) { \
long long b = clock(); \
func;\
long long e = clock(); \
printf("%s run %lldms\n", #func, 1000 * (e - b) / CLOCKS_PER_SEC); \
}
BEGIN(normal_count)
int is_prime(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
int main() {
int cnt = 0;
for (int i = 2; i <= MAX_N; i++) {
cnt += is_prime(i);
}
cout << cnt << endl;
return 0;
}
END(normal_count)
BEGIN(prime_count_1)
int is_prime(int x) {
if (x <= 1) return 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
void count_prime(int b, int e, int &ret) {
ret = 0;
for (int i = b; i <= e; i++) {
ret += is_prime(i);
}
return ;
}
int main() {
int batch = MAX_N / 10;
int ret[10];
thread *threads[10];
for (int i = 0, j = 1; i < 10; i++, j += batch) {
threads[i] = new thread(count_prime, j, j + batch - 1, ref(ret[i]));
}
for (int i = 0; i < 10; i++) {
threads[i]->join();
}
int ans = 0;
for (auto x : ret) ans += x;
cout << ans << endl;
return 0;
}
END(prime_count_1)
BEGIN(prime_count_2)
int ans;
mutex mutex_x;
int is_prime(int x) {
if (x <= 1) return 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
void count_prime(int b, int e) {
for (int i = b; i <= e; i++) {
unique_lock<mutex> lock(mutex_x);
ans += is_prime(i);
}
return ;
}
int main() {
int batch = MAX_N / 10;
int ret[10];
thread *threads[10];
ans = 0;
for (int i = 0, j = 1; i < 10; i++, j += batch) {
threads[i] = new thread(count_prime, j, j + batch - 1);
}
for (int i = 0; i < 10; i++) {
threads[i]->join();
}
cout << ans << endl;
return 0;
}
END(prime_count_2)
BEGIN(prime_count_3)
int ans;
int is_prime(int x) {
if (x <= 1) return 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
void count_prime(int b, int e) {
for (int i = b; i <= e; i++) {
int ret = is_prime(i);
__sync_fetch_and_add(&ans, ret);
}
return ;
}
int main() {
int batch = MAX_N / 10;
int ret[10];
thread *threads[10];
ans = 0;
for (int i = 0, j = 1; i < 10; i++, j += batch) {
threads[i] = new thread(count_prime, j, j + batch - 1);
}
for (int i = 0; i < 10; i++) {
threads[i]->join();
}
cout << ans << endl;
return 0;
}
END(prime_count_3)
int main() {
//normal_count::main();
//prime_count_1::main();
//prime_count_2::main();
prime_count_3::main();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
1.【第10期】专属内容.zip (6个子文件)
1.【第10期】专属内容
.DS_Store 6KB
2.prime_count.cpp 3KB
a.out 192KB
1.thread.cpp 1KB
3.bind.cpp 2KB
4.thread_pool.cpp 3KB
共 6 条
- 1
资源评论
辰溪爱吃小面包
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功