c++ 并发处理
来源:互联网 发布:淘宝邮箱在哪里 编辑:程序博客网 时间:2024/06/04 01:07
#include <windows.h>
#include <ppl.h>
#include <concurrent_vector.h>
#include <array>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iostream>
using namespace concurrency;
using namespace std;
// Calls the provided work function and returns the number of milliseconds
// that it takes to call that function.
template <class Function>
__int64 time_call(Function&& f)
{
__int64 begin = GetTickCount();
f();
return GetTickCount() - begin;
}
// Computes the nth Fibonacci number.
int fibonacci(int n)
{
if(n < 2)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int wmain()
{
__int64 elapsed;
// An array of Fibonacci numbers to compute.
array<int, 4> a = { 24, 26, 41, 42 };
// The results of the serial computation.
vector<tuple<int,int>> results1;
// The results of the parallel computation.
concurrent_vector<tuple<int,int>> results2;
// Use the for_each algorithm to compute the results serially.
elapsed = time_call([&]
{
for_each (begin(a), end(a), [&](int n) {
results1.push_back(make_tuple(n, fibonacci(n)));
});
});
wcout << L"serial time: " << elapsed << L" ms" << endl;
// Use the parallel_for_each algorithm to perform the same task.
elapsed = time_call([&]
{
parallel_for_each (begin(a), end(a), [&](int n) {
results2.push_back(make_tuple(n, fibonacci(n)));
});
// Because parallel_for_each acts concurrently, the results do not
// have a pre-determined order. Sort the concurrent_vector object
// so that the results match the serial version.
sort(begin(results2), end(results2));
});
wcout << L"parallel time: " << elapsed << L" ms" << endl << endl;
// Print the results.
for_each (begin(results2), end(results2), [](tuple<int,int>& pair) {
wcout << L"fib(" << get<0>(pair) << L"): " << get<1>(pair) << endl;
});
}
#include <ppl.h>
#include <concurrent_vector.h>
#include <array>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iostream>
using namespace concurrency;
using namespace std;
// Calls the provided work function and returns the number of milliseconds
// that it takes to call that function.
template <class Function>
__int64 time_call(Function&& f)
{
__int64 begin = GetTickCount();
f();
return GetTickCount() - begin;
}
// Computes the nth Fibonacci number.
int fibonacci(int n)
{
if(n < 2)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int wmain()
{
__int64 elapsed;
// An array of Fibonacci numbers to compute.
array<int, 4> a = { 24, 26, 41, 42 };
// The results of the serial computation.
vector<tuple<int,int>> results1;
// The results of the parallel computation.
concurrent_vector<tuple<int,int>> results2;
// Use the for_each algorithm to compute the results serially.
elapsed = time_call([&]
{
for_each (begin(a), end(a), [&](int n) {
results1.push_back(make_tuple(n, fibonacci(n)));
});
});
wcout << L"serial time: " << elapsed << L" ms" << endl;
// Use the parallel_for_each algorithm to perform the same task.
elapsed = time_call([&]
{
parallel_for_each (begin(a), end(a), [&](int n) {
results2.push_back(make_tuple(n, fibonacci(n)));
});
// Because parallel_for_each acts concurrently, the results do not
// have a pre-determined order. Sort the concurrent_vector object
// so that the results match the serial version.
sort(begin(results2), end(results2));
});
wcout << L"parallel time: " << elapsed << L" ms" << endl << endl;
// Print the results.
for_each (begin(results2), end(results2), [](tuple<int,int>& pair) {
wcout << L"fib(" << get<0>(pair) << L"): " << get<1>(pair) << endl;
});
}
0 0
- 并发处理
- 并发处理
- 并发处理
- 并发处理
- 并发处理
- 并发处理
- 处理并发
- 并发处理
- Sql Server 并发处理
- 多线程并发处理
- 数据库并发处理
- FastCGI的并发处理
- FastCGI的并发处理
- 乐观锁(处理并发)
- 大并发处理解决方案
- java处理多线程并发
- 服务器并发处理能力
- 并发处理解决方案
- 斐波那契数列
- Callable,Runnable比较及用法
- android 圆形头像
- hibernate 三种状态详解
- C++构造函数初始化列表与赋值
- c++ 并发处理
- 在JS文件中引用JS
- What exactly is "iowait"?
- 在Windows下使用Visual Studio编译Lua5.3
- U-boot 参数恢复
- Eclipse 修改项目名称
- Nginx开启Gzip压缩大幅提高页面加载速度及相关测试
- Memcached-学习(二)XMemcached&Spring集成
- 黑马程序员——java基础知识回顾(一)(java语言基础组成)