Guest User

Untitled

a guest
Mar 12th, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. /* foreach vs. vector loop speed test */
  2.  
  3. #include <a_samp>
  4. #include <cstl>
  5. #define FILTERSCRIPT
  6. #include <foreach>
  7.  
  8. #define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
  9. while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
  10. __e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
  11. GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
  12.  
  13. #define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
  14. %0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
  15.  
  16. new const VEC = 0;
  17.  
  18. public OnFilterScriptInit()
  19. {
  20. new Iterator:MyIterator<50>; // initialize foreach iterator
  21.  
  22. for (new i = 0 ; i < 50 ; i++) // fill vector and iterator with equal integers
  23. {
  24. vector_push_back(VEC, i);
  25. Iter_Add(MyIterator, i);
  26. }
  27. new vari, size, x;
  28.  
  29. START_BENCH(1000);
  30. foreach (MyIterator, var)
  31. {
  32. x = var + 1;
  33. }
  34. FINISH_BENCH("foreach");
  35.  
  36. printf("foreach ran %i times",x);
  37. x = 0;
  38.  
  39.  
  40. size = vector_size(VEC);
  41.  
  42. START_BENCH(1000);
  43. for (new i = 0 ; i < size ; i++)
  44. {
  45. vari = vector_get(VEC, i);
  46. x = vari + 1;
  47. }
  48. FINISH_BENCH("vector loop");
  49.  
  50. printf("vector loop ran %i times",x);
  51. return 1;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment