#import <Foundation/Foundation.h>

double GetSecs( void )
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    
    return tv.tv_sec + tv.tv_usec / 1000000.0;
}

void TestAutoreleaseSpeed( int tries, int objs, int iters )
{
    int i, j, k;
    
    printf( "Testing %d iterations, allocating %d objects per iteration...", iters, objs );
    fflush( stdout );
    
    double total = 0.0;
    
    for( i = 0; i < tries; i++ )
    {
        double startTime, endTime;
        
        startTime = GetSecs();
        
        for( j = 0; j < iters; j++ )
        {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            
            for( k = 0; k < objs; k++ )
                [[[NSObject alloc] init] autorelease];
            
            [pool release];
        }
        
        endTime = GetSecs();
        total += endTime - startTime;
    }
    
    printf( "done\ntotal time: %f seconds\n\n", total / tries );
    fflush( stdout );
}

int main(int argc, char **argv)
{
    printf( "===== warming up whatever caches may exist, ignore this\n" );
    TestAutoreleaseSpeed( 1,  1,  1000 );
    printf( "===== done, results follow\n\n\n" );
    
    int i;
    for( i = 1; i <= 1000000; i *= 10 )
        TestAutoreleaseSpeed( 10, 1000000 / i, i );
}
