【发布时间】:2014-07-11 11:43:47
【问题描述】:
所以我运行了一段代码,我想提高性能,我注意到删除需要很长时间才能完成(大约 0.003 秒),所以我决定把它放到另一个线程中,然后删除数组。
现在创建和运行线程所需的时间比删除数组要快得多,但是现在我创建线程后的代码性能受到了影响,运行时间大约延长了 2 到 3 倍。
有谁知道为什么会发生这种情况以及如何提高我遇到的性能问题?请注意,我使用的是 dispatch_async,因为我之前只在 mac 上编写过代码,并且没有尝试过创建多个线程的其他 C/C++ 库,所以如果有人知道可以以更好的性能做同样事情的替代库,那么我将切换到使用它。
clock_t start, end, start2, end2;
start = clock();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
std::set<int> *temp = a;
a = nullptr;
dispatch_async(queue, ^{
delete[] temp;
});
//delete[] a;
end = clock();
a = new std::set<int>[10000];
start2 = clock();
/*
Code here initializes stuff inside the array a
Code here never changes
*/
end2 = clock();
//(end2 - start2/CLOCKS_PER_SEC) is now much longer than it was without multithreading but (end - start)/CLOCKS_PER_SEC is much faster (which is expected)
【问题讨论】:
标签: c++ objective-c multithreading performance grand-central-dispatch