Guest User

Untitled

a guest
May 24th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2.  
  3. #include <mach/mach_time.h>
  4. #include <stdint.h>
  5.  
  6.  
  7. void stringTest()
  8. {
  9. mach_timebase_info_data_t info;
  10. mach_timebase_info(&info);
  11.  
  12. uint64_t startTime;
  13. uint64_t endTime;
  14.  
  15. NSMutableArray *shortStringPool=[NSMutableArray arrayWithCapacity:10000];
  16. NSMutableArray *longStringPool=[NSMutableArray arrayWithCapacity:10000];
  17. NSMutableDictionary *shortDictionary10k=[NSMutableDictionary dictionary];
  18. NSMutableDictionary *shortDictionary200=[NSMutableDictionary dictionary];
  19. NSMutableDictionary *longDictionary10k=[NSMutableDictionary dictionary];
  20. NSMutableDictionary *longDictionary200=[NSMutableDictionary dictionary];
  21.  
  22. for(int i=0; i<10000; i++)
  23. {
  24. NSString *shortString=[NSString stringWithFormat:@"0123456789%d", i];
  25. [shortString hash];
  26. [shortStringPool addObject:shortString];
  27.  
  28. NSString *longString=[NSString stringWithFormat:@"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789%d", i];
  29. [longString hash];
  30. [longStringPool addObject:longString];
  31. };
  32.  
  33. // Short string hash test, 10000 each.
  34. startTime = mach_absolute_time();
  35.  
  36. for(int i=0; i<10000; i++)
  37. {
  38. [[shortStringPool objectAtIndex:i] hash];
  39. };
  40.  
  41. endTime = mach_absolute_time();
  42.  
  43. printf("Short strings, hash test, 10000 each: %f\n", (((double)(endTime-startTime))*((double)info.numer/(double)info.denom))/10000.0);
  44.  
  45. // Long string hash test, 10000 each.
  46. startTime = mach_absolute_time();
  47.  
  48. for(int i=0; i<10000; i++)
  49. {
  50. [[longStringPool objectAtIndex:i] hash];
  51. };
  52.  
  53. endTime = mach_absolute_time();
  54.  
  55. printf("Long strings, hash test, 10000 each: %f\n", (((double)(endTime-startTime))*((double)info.numer/(double)info.denom))/10000.0);
  56.  
  57. // Short string test, 10000 each.
  58. for(int i=0; i<10000; i++)
  59. {
  60. [shortDictionary10k setObject:[NSNull null] forKey:[shortStringPool objectAtIndex:i]];
  61. };
  62.  
  63. startTime = mach_absolute_time();
  64.  
  65. for(int i=0; i<10000; i++)
  66. {
  67. [shortDictionary10k objectForKey:[shortStringPool objectAtIndex:i]];
  68. };
  69.  
  70. endTime = mach_absolute_time();
  71.  
  72. printf("Short strings, 10000 each: %f\n", (((double)(endTime-startTime))*((double)info.numer/(double)info.denom))/10000.0);
  73.  
  74. // Short string test, 200 each.
  75. for(int i=0; i<200; i++)
  76. {
  77. [shortDictionary200 setObject:[NSNull null] forKey:[shortStringPool objectAtIndex:i]];
  78. };
  79.  
  80. startTime = mach_absolute_time();
  81.  
  82. for(int i=0; i<200; i++)
  83. {
  84. [shortDictionary200 objectForKey:[shortStringPool objectAtIndex:i]];
  85. };
  86.  
  87. endTime = mach_absolute_time();
  88.  
  89. printf("Short strings, 200 each: %f\n", (((double)(endTime-startTime))*((double)info.numer/(double)info.denom))/200.0);
  90.  
  91. // Long string test, 10000 each.
  92. for(int i=0; i<10000; i++)
  93. {
  94. [longDictionary10k setObject:[NSNull null] forKey:[longStringPool objectAtIndex:i]];
  95. };
  96.  
  97. startTime = mach_absolute_time();
  98.  
  99. for(int i=0; i<10000; i++)
  100. {
  101. [longDictionary10k objectForKey:[longStringPool objectAtIndex:i]];
  102. };
  103.  
  104. endTime = mach_absolute_time();
  105.  
  106. printf("Long strings, 10000 each: %f\n", (((double)(endTime-startTime))*((double)info.numer/(double)info.denom))/10000.0);
  107.  
  108. // Long string test, 200 each.
  109. for(int i=0; i<200; i++)
  110. {
  111. [longDictionary200 setObject:[NSNull null] forKey:[longStringPool objectAtIndex:i]];
  112. };
  113.  
  114. startTime = mach_absolute_time();
  115. for(int i=0; i<200; i++)
  116. {
  117. [longDictionary200 objectForKey:[longStringPool objectAtIndex:i]];
  118. };
  119.  
  120. endTime = mach_absolute_time();
  121.  
  122. printf("Long strings, 200 each: %f\n\n", (((double)(endTime-startTime))*((double)info.numer/(double)info.denom))/200.0);
  123. };
  124.  
  125. int main(int argc, const char * argv[])
  126. {
  127. @autoreleasepool
  128. {
  129. for(int i=0; i<10; i++) stringTest();
  130. };
  131. return 0;
  132. };
Advertisement
Add Comment
Please, Sign In to add comment