Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* foreach vs. vector loop speed test */
- #include <a_samp>
- #include <cstl>
- #define FILTERSCRIPT
- #include <foreach>
- #define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
- while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
- __e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
- GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
- #define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
- %0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
- new const VEC = 0;
- public OnFilterScriptInit()
- {
- new Iterator:MyIterator<50>; // initialize foreach iterator
- for (new i = 0 ; i < 50 ; i++) // fill vector and iterator with equal integers
- {
- vector_push_back(VEC, i);
- Iter_Add(MyIterator, i);
- }
- new vari, size, x;
- START_BENCH(1000);
- foreach (MyIterator, var)
- {
- x = var + 1;
- }
- FINISH_BENCH("foreach");
- printf("foreach ran %i times",x);
- x = 0;
- size = vector_size(VEC);
- START_BENCH(1000);
- for (new i = 0 ; i < size ; i++)
- {
- vari = vector_get(VEC, i);
- x = vari + 1;
- }
- FINISH_BENCH("vector loop");
- printf("vector loop ran %i times",x);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment