小编Bre*_*ddy的帖子

性能测试:sem_t vs dispatch_semaphore_t和pthread_once_t vs dispatch_once_t

我想知道使用POSIX调用pthread_once()和/ sem_wait()或dispatch_*函数会更好/更快,所以我创建了一个小测试并对结果感到惊讶(问题和结果在最后).

在测试代​​码中,我使用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) …
Run Code Online (Sandbox Code Playgroud)

multithreading semaphore objective-c grand-central-dispatch

14
推荐指数
1
解决办法
6001
查看次数

NSFetchedResultsController与UILocalizedIndexedCollat​​ion

我正在尝试使用具有混合语言数据的FRC并希望有一个节索引.

似乎从文档中你应该能够覆盖FRC

- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName
- (NSArray *)sectionIndexTitles
Run Code Online (Sandbox Code Playgroud)

然后使用UILocalizedIndexedCollat​​ion来获得本地化的索引和节.但遗憾的是,这不起作用,并不是打算使用的:(

有没有人能够使用带有UILocalizedIndexedCollat​​ion的FRC,或者我们被迫使用示例UITableView + UILocalizedIndexedCollat​​ion示例中提到的手动排序方法(示例代码包含在我工作的地方).

使用以下属性

@property (nonatomic, assign) UILocalizedIndexedCollation *collation;
@property (nonatomic, assign) NSMutableArray *collatedSections;
Run Code Online (Sandbox Code Playgroud)

和代码:

- (UILocalizedIndexedCollation *)collation
{
    if(collation == nil)
    {
        collation = [UILocalizedIndexedCollation currentCollation];
    }

    return collation;
}

- (NSArray *)collatedSections
{
    if(_collatedSections == nil)
    {
        int sectionTitlesCount = [[self.collation sectionTitles] count];

        NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
        collatedSections = newSectionsArray;
        NSMutableArray *sectionsCArray[sectionTitlesCount];

        // Set up the sections array: elements are mutable arrays that will contain the …
Run Code Online (Sandbox Code Playgroud)

localization core-data nsfetchedresultscontroller ios uilocalizedcollation

14
推荐指数
2
解决办法
6698
查看次数