我想知道使用像pthread_once()
andsem_wait()
或 dispatch_* 函数这样的 POSIX 调用会更好/更快,所以我创建了一个小测试并对结果感到惊讶(问题和结果在最后)。
在测试代码中,我使用 mach_absolute_time() 来计时调用。我真的不在乎这与纳秒不完全匹配。我正在将这些值相互比较,因此确切的时间单位无关紧要,只有间隔之间的差异才重要。结果部分中的数字是可重复的,不是平均的;我本可以平均时间,但我不是在寻找确切的数字。
test.m(简单的控制台应用程序;易于编译):
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#include <semaphore.h>
#include <pthread.h>
#include <time.h>
#include <mach/mach_time.h>
// *sigh* OSX does not have pthread_barrier (you can ignore the pthread_barrier
// code, the interesting stuff is lower)
typedef int pthread_barrierattr_t;
typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t cond;
int count;
int tripCount;
} pthread_barrier_t;
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
{
if(count == 0)
{
errno = EINVAL;
return -1;
}
if(pthread_mutex_init(&barrier->mutex, 0) < 0)
{
return -1;
}
if(pthread_cond_init(&barrier->cond, 0) < 0)
{
pthread_mutex_destroy(&barrier->mutex);
return -1;
}
barrier->tripCount = count;
barrier->count = 0;
return 0;
}
int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
pthread_cond_destroy(&barrier->cond);
pthread_mutex_destroy(&barrier->mutex);
return 0;
}
int pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
++(barrier->count);
if(barrier->count >= barrier->tripCount)
{
barrier->count = 0;
pthread_cond_broadcast(&barrier->cond);
pthread_mutex_unlock(&barrier->mutex);
return 1;
}
else
{
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
pthread_mutex_unlock(&barrier->mutex);
return 0;
}
}
//
// ok you can start paying attention now...
//
void onceFunction(void)
{
}
@interface SemaphoreTester : NSObject
{
sem_t *sem1;
sem_t *sem2;
pthread_barrier_t *startBarrier;
pthread_barrier_t *finishBarrier;
}
@property (nonatomic, assign) sem_t *sem1;
@property (nonatomic, assign) sem_t *sem2;
@property (nonatomic, assign) pthread_barrier_t *startBarrier;
@property (nonatomic, assign) pthread_barrier_t *finishBarrier;
@end
@implementation SemaphoreTester
@synthesize sem1, sem2, startBarrier, finishBarrier;
- (void)thread1
{
pthread_barrier_wait(startBarrier);
for(int i = 0; i < 100000; i++)
{
sem_wait(sem1);
sem_post(sem2);
}
pthread_barrier_wait(finishBarrier);
}
- (void)thread2
{
pthread_barrier_wait(startBarrier);
for(int i = 0; i < 100000; i++)
{
sem_wait(sem2);
sem_post(sem1);
}
pthread_barrier_wait(finishBarrier);
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int64_t start;
int64_t stop;
// semaphore non contention test
{
// grrr, OSX doesn't have sem_init
sem_t *sem1 = sem_open("sem1", O_CREAT, 0777, 0);
start = mach_absolute_time();
for(int i = 0; i < 100000; i++)
{
sem_post(sem1);
sem_wait(sem1);
}
stop = mach_absolute_time();
sem_close(sem1);
NSLog(@"0 Contention time = %d", stop - start);
}
// semaphore contention test
{
__block sem_t *sem1 = sem_open("sem1", O_CREAT, 0777, 0);
__block sem_t *sem2 = sem_open("sem2", O_CREAT, 0777, 0);
__block pthread_barrier_t startBarrier;
pthread_barrier_init(&startBarrier, NULL, 3);
__block pthread_barrier_t finishBarrier;
pthread_barrier_init(&finishBarrier, NULL, 3);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(queue, ^{
pthread_barrier_wait(&startBarrier);
for(int i = 0; i < 100000; i++)
{
sem_wait(sem1);
sem_post(sem2);
}
pthread_barrier_wait(&finishBarrier);
});
dispatch_async(queue, ^{
pthread_barrier_wait(&startBarrier);
for(int i = 0; i < 100000; i++)
{
sem_wait(sem2);
sem_post(sem1);
}
pthread_barrier_wait(&finishBarrier);
});
pthread_barrier_wait(&startBarrier);
// start timing, everyone hit this point
start = mach_absolute_time();
// kick it off
sem_post(sem2);
pthread_barrier_wait(&finishBarrier);
// stop timing, everyone hit the finish point
stop = mach_absolute_time();
sem_close(sem1);
sem_close(sem2);
NSLog(@"2 Threads always contenting time = %d", stop - start);
pthread_barrier_destroy(&startBarrier);
pthread_barrier_destroy(&finishBarrier);
}
// NSTask semaphore contention test
{
sem_t *sem1 = sem_open("sem1", O_CREAT, 0777, 0);
sem_t *sem2 = sem_open("sem2", O_CREAT, 0777, 0);
pthread_barrier_t startBarrier;
pthread_barrier_init(&startBarrier, NULL, 3);
pthread_barrier_t finishBarrier;
pthread_barrier_init(&finishBarrier, NULL, 3);
SemaphoreTester *tester = [[[SemaphoreTester alloc] init] autorelease];
tester.sem1 = sem1;
tester.sem2 = sem2;
tester.startBarrier = &startBarrier;
tester.finishBarrier = &finishBarrier;
[NSThread detachNewThreadSelector:@selector(thread1) toTarget:tester withObject:nil];
[NSThread detachNewThreadSelector:@selector(thread2) toTarget:tester withObject:nil];
pthread_barrier_wait(&startBarrier);
// start timing, everyone hit this point
start = mach_absolute_time();
// kick it off
sem_post(sem2);
pthread_barrier_wait(&finishBarrier);
// stop timing, everyone hit the finish point
stop = mach_absolute_time();
sem_close(sem1);
sem_close(sem2);
NSLog(@"2 NSTasks always contenting time = %d", stop - start);
pthread_barrier_destroy(&startBarrier);
pthread_barrier_destroy(&finishBarrier);
}
// dispatch_semaphore non contention test
{
dispatch_semaphore_t sem1 = dispatch_semaphore_create(0);
start = mach_absolute_time();
for(int i = 0; i < 100000; i++)
{
dispatch_semaphore_signal(sem1);
dispatch_semaphore_wait(sem1, DISPATCH_TIME_FOREVER);
}
stop = mach_absolute_time();
NSLog(@"Dispatch 0 Contention time = %d", stop - start);
}
// dispatch_semaphore non contention test
{
__block dispatch_semaphore_t sem1 = dispatch_semaphore_create(0);
__block dispatch_semaphore_t sem2 = dispatch_semaphore_create(0);
__block pthread_barrier_t startBarrier;
pthread_barrier_init(&startBarrier, NULL, 3);
__block pthread_barrier_t finishBarrier;
pthread_barrier_init(&finishBarrier, NULL, 3);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(queue, ^{
pthread_barrier_wait(&startBarrier);
for(int i = 0; i < 100000; i++)
{
dispatch_semaphore_wait(sem1, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(sem2);
}
pthread_barrier_wait(&finishBarrier);
});
dispatch_async(queue, ^{
pthread_barrier_wait(&startBarrier);
for(int i = 0; i < 100000; i++)
{
dispatch_semaphore_wait(sem2, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(sem1);
}
pthread_barrier_wait(&finishBarrier);
});
pthread_barrier_wait(&startBarrier);
// start timing, everyone hit this point
start = mach_absolute_time();
// kick it off
dispatch_semaphore_signal(sem2);
pthread_barrier_wait(&finishBarrier);
// stop timing, everyone hit the finish point
stop = mach_absolute_time();
NSLog(@"Dispatch 2 Threads always contenting time = %d", stop - start);
pthread_barrier_destroy(&startBarrier);
pthread_barrier_destroy(&finishBarrier);
}
// pthread_once time
{
pthread_once_t once = PTHREAD_ONCE_INIT;
start = mach_absolute_time();
for(int i = 0; i <100000; i++)
{
pthread_once(&once, onceFunction);
}
stop = mach_absolute_time();
NSLog(@"pthread_once time = %d", stop - start);
}
// dispatch_once time
{
dispatch_once_t once = 0;
start = mach_absolute_time();
for(int i = 0; i <100000; i++)
{
dispatch_once(&once, ^{});
}
stop = mach_absolute_time();
NSLog(@"dispatch_once time = %d", stop - start);
}
[pool drain];
return 0;
}
在我的 iMac(雪豹服务器 10.6.4)上:
型号标识符:iMac7,1 处理器名称:Intel Core 2 Duo 处理器速度:2.4 GHz 处理器数量:1 核心总数:2 二级缓存:4 MB 内存:4 GB 总线速度:800 MHz
我得到:
0 争用时间 = 101410439 2 线程总是满足时间 = 109748686 2 NSTasks 总是满足时间 = 113225207 0 争用命名信号量时间 = 166061832 2 个线程命名信号量争用时间 = 203913476 2 个名为 semaphore 的 NSTasks 争用时间 = 204988744 调度 0 争用时间 = 3411439 调度 2 线程总是满足时间 = 708073977 pthread_once 时间 = 2707770 dispatch_once 时间 = 87433
在我的 MacbookPro(雪豹 10.6.4)上:
型号标识符:MacBookPro6,2 处理器名称:英特尔酷睿 i5 处理器速度:2.4 GHz 处理器数量:1 核心总数:2(虽然启用了 HT) L2 缓存(每核):256 KB 三级缓存:3 MB 内存:8 GB 处理器互连速度:4.8 GT/s
我有:
0 争用时间 = 74172042 2 线程总是满足时间 = 82975742 2 NSTasks 总是满足时间 = 82996716 0 争用命名信号量时间 = 106772641 2 线程命名信号量争用时间 = 162761973 2 个名为 semaphore 的 NSTasks 争用时间 = 162919844 调度 0 争用时间 = 1634941 调度 2 线程总是满足时间 = 759753865 pthread_once 时间 = 1516787 dispatch_once 时间 = 120778
在 iPhone 3GS 4.0.2 上,我得到:
0 争用时间 = 5971929 2 线程总是满足时间 = 11989710 2 NSTasks 总是满足时间 = 11950564 0 争用命名信号量时间 = 16721876 2 线程命名信号量争用时间 = 35333045 2 个名为 semaphore 的 NSTasks 争用时间 = 35296579 调度 0 争用时间 = 151909 调度 2 线程总是满足时间 = 46946548 pthread_once 时间 = 193592 dispatch_once 时间 = 25071
问题和陈述:
sem_wait()
并且sem_post()
在没有争用时很慢- 为什么会这样?
- OSX 不关心兼容的 API 吗?是否有一些遗留代码迫使它变慢?
- 为什么这些数字与 dispatch_semaphore 函数不同?
sem_wait()
并且sem_post()
在争用时和不在争用时一样慢(存在差异,但我认为在争用和非争用之间会有巨大差异;我期望像 dispatch_semaphore 代码中那样的数字)sem_wait()
并且sem_post()
在使用命名信号量时速度较慢。- 为什么?这是因为信号量必须在进程之间同步吗?这样做可能会有更多的包袱。
dispatch_semaphore_wait()
并且dispatch_semaphore_signal()
在没有争议的情况下很快就会发疯(这并不奇怪,因为苹果经常吹捧这一点)。dispatch_semaphore_wait()
并且dispatch_semaphore_signal()
比竞争时sem_wait()
慢3 倍sem_post()
- 为什么这么慢?这对我来说没有意义。我本来希望这与争论中的 sem_t 相提并论。
dispatch_once()
比 快pthread_once()
,大约 10 倍,为什么?我从标题中唯一能看出的是 withdispatch_once()
比 with没有函数调用负担pthread_once()
。
动机: 我得到了两套工具来完成信号量或一次调用的工作(实际上我同时发现了其他信号量变体,但除非作为更好的选择提出,否则我会忽略它们)。我只是想知道什么是最适合这项工作的工具(如果您可以选择用飞利浦或平头螺丝拧入螺丝,如果我不必拧紧螺丝和平头,我会选择飞利浦)拧紧螺丝)。看来,如果我开始使用 libdispatch 编写实用程序,我可能无法将它们移植到其他还没有 libdispatch 工作的操作系统……但使用起来非常诱人;)
就目前而言:当我不必担心可移植性和 POSIX 调用时,我将使用 libdispatch。
谢谢!