Re: How to count Atoms
Re: How to count Atoms
- Subject: Re: How to count Atoms
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 08 Oct 2016 12:06:03 +0700
> On 8 Oct 2016, at 11:22, Quincey Morris <email@hidden> wrote:
>
> On Oct 7, 2016, at 21:06 , Gerriet M. Denkmann <email@hidden> wrote:
>>
>> But, alas, it is also much slower: overhead almost 40 sec (whereas my admittedly rather hackish way took less then half a second).
>
> That may indicate a lot of contention — e.g. you were running multiple copies of that loop in different threads, and each loop was doing an increment every iteration. I’d actually try doing it this way:
>
>> // One-time initialization, globally
>>
>> dsema = dispatch_semaphore_create (1);
>>
>> NSUInteger counter = 0;
>>
>>
>> // Per-thread code, locally
>>
>> NSUInteger perThreadCounter = 0;
>>
>> for …
>> {
>> …
>> perThreadCounter++;
>> }
>>
>> dispatch_semaphore_wait (dsema, <forever>);
>> counter += perThreadCounter;
>> dispatch_semaphore_signal (dsema);
>
> That should diminish the overhead to, effectively, zero.
Well, it diminishes the overhead from about 40 sec to less than 60 msec - a huge improvement indeed.
But, using the same perThreadCounter, atomic_fetch_add_explicit has an overhead of less than 2 msec.
I really should have thought of an perThreadCounter myself - it is obviously so much better.
Thanks for pointing me in the right direction!
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden