Repository: realm/realm-swift
File: examples/RealmPerformanceExample/RealmPerformanceExample/Performance.m (Objective-C)

Code before:
}

-(void)dealloc
{
    sqlite3_close(db);
    db = NULL;
}


-(void)reportSizeForFile:(NSString *)file msg:(NSString *)msg
{
    NSDictionary *fileAttributes = [NSFileManager.defaultManager attributesOfItemAtPath:file error:nil];
    NSString *fileSize = [fileAttributes objectForKey:NSFileSize];
    dispatch_async(dispatch_get_main_queue(), ^{
        [_utils OutGroup:GROUP_SIZE msg:[NSString stringWithFormat:@"%@: %@", msg, fileSize]];
    });
}

- (void)testInsert {
    
    NSUInteger count = _size;
    
    [[NSFileManager defaultManager] removeItemAtPath:[_utils pathForDataFile:@"perfemployees.realm"] error:nil];

    NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

    RLMRealm *manager = [RLMRealm realmWithPath:[_utils pathForDataFile:@"perfemployees.realm"] error:nil];
    [manager writeUsingBlock:^(RLMRealm *realm) {
        // Create new table in realm
        PerfTable *table = [PerfTable tableInRealm:realm named:@"employees"];
        
        // Add some rows
        for (NSUInteger i = 0; i < count; i++) {
            [table addRow:nil];
            PerfObj *perf = table.lastRow;
            perf.name = @"Foo";
            perf.age = (25 + (int)(drand48() * 4));
            perf.hired = YES;
            perf.spare = 0;
        }
        [table addRow:nil];
        PerfObj *perf = table.lastRow;
        perf.name = @"Sparse";
        perf.age = 41;
        perf.hired = NO;
        perf.spare = 2;
        
        //NSLog(@"Age verify: %lld", [table RLM_intInColumnWithIndex:1 atRowIndex:1000]);

    }];


Review comment (suggestion):
Please consider setting the seed for `drand48` for test becomes deterministic.


Code after:


-(void)reportSizeForFile:(NSString *)file msg:(NSString *)msg
{
    NSDictionary *fileAttributes = [NSFileManager.defaultManager attributesOfItemAtPath:file error:nil];
    NSString *fileSize = [fileAttributes objectForKey:NSFileSize];
    dispatch_async(dispatch_get_main_queue(), ^{
        [_utils OutGroup:GROUP_SIZE msg:[NSString stringWithFormat:@"%@: %@", msg, fileSize]];
    });
}

- (void)testInsert {
    
    NSUInteger count = _size;
    
    [[NSFileManager defaultManager] removeItemAtPath:[_utils pathForDataFile:@"perfemployees.realm"] error:nil];
    NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

    RLMRealm *realm = [RLMRealm realmWithPath:[_utils pathForDataFile:@"perfemployees.realm"]];
    [realm beginWriteTransaction];
    
        // Add some rows
    for (NSUInteger i = 0; i < count; i++) {
        PerfObj *perf = [[PerfObj alloc] init];
        perf.name = @"Foo";
        perf.age = (25 + (int)(drand48() * 4));
        perf.hired = YES;
        perf.spare = 0;
        [realm addObject:perf];
    }
    
    PerfObj *perf = [[PerfObj alloc] init];
    perf.name = @"Sparse";
    perf.age = 41;
    perf.hired = NO;
    perf.spare = 2;
    
    [realm addObject:perf];
    
    [realm commitWriteTransaction];

    NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate];
    dispatch_async(dispatch_get_main_queue(), ^{
        [_utils OutGroup:GROUP_RUN msg:[NSString stringWithFormat:@"RLM inserted %i records in %.2f s",_size, stop-start]];
    });



    NSTimeInterval sqlstart = [NSDate timeIntervalSinceReferenceDate];
    char *zErrMsg = NULL;
    db = NULL;