Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / functor, loki or boost? Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Pet123

March 19, 2005, 01:16 PM

hi there
im planning to use functors in my project, as i know there are two options out there : loki and boost, its hard for me to decide which one to use, so i did a rough test today, the result shows in some cases loki wins and other cases boost beats loki. in detail, boost will win if allcates occurs quite often. the reason for this is because functor in loki is derived from SmallObj, this type of objects are organised in a vector, every allocation may cause a search operation in that vector. here is the test results :

1st test :
loki : b = 74604000, [47422 ms]
boost : b = 74604000, [39625 ms]

2nd test :
loki : b = 74604000, [47437 ms]
boost : b = 74604000, [39641 ms]

3rd test :
loki : b = 74604000, [47421 ms]
boost : b = 74604000, [39641 ms]

and here is the code :

  1.  
  2.  
  3. #include <loki/functor.h>
  4. #include <boost/function.hpp>
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <windows.h>
  11. #include <mmsystem.h>
  12.  
  13. #pragma comment(lib, "winmm.lib")
  14.  
  15. int func(int var)
  16. {
  17.         return (var >> 1) % 30;
  18. }
  19.  
  20. struct X
  21. {
  22.         int v;
  23. public :
  24.         X() : v(0) {}
  25.  
  26. public :
  27.         void foo(int var)
  28.         {
  29.                 v += var;
  30.                 v /= 7;
  31.         }
  32. };
  33.  
  34. void main()
  35. {
  36.         /// loki go first
  37.         {
  38.                 using namespace Loki;
  39.  
  40.                 X x;
  41.  
  42.                 DWORD curtime = timeGetTime();
  43.  
  44.                 int a = 0;
  45.                 int b = 0;
  46.                 for (int i = 0; i < 2000; ++i)
  47.                 {
  48.                         a = 0;
  49.                         for (int j = 0; j < 20000; ++j)
  50.                         {
  51.                                 Functor<int, TYPELIST_1(int)> f(func);
  52.                                 Functor<void, TYPELIST_1(int)> mf(&x, &X::foo);
  53.  
  54.                                 const int r = f(j);
  55.                                 mf(r);
  56.                                 a += x.v;
  57.                         }
  58.  
  59.                         b += a;
  60.                         x.v = 0;
  61.                 }
  62.  
  63.                 curtime = timeGetTime() - curtime;
  64.  
  65.                 cout << "loki  : b = " << b << ", [" << curtime << " ms]" << endl << endl;
  66.         }
  67.  
  68.         /// now boost
  69.         {
  70.                 using namespace boost;
  71.  
  72.                 X x;
  73.  
  74.                 DWORD curtime = timeGetTime();
  75.  
  76.                 int a = 0;
  77.                 int b = 0;
  78.                 for (int i = 0; i < 2000; ++i)
  79.                 {
  80.                         a = 0;
  81.                         for (int j = 0; j < 20000; ++j)
  82.                         {
  83.                                 boost::function<int (int)> f(&func);
  84.                                 boost::function<void (X *, int)> mf(&X::foo);
  85.  
  86.                                 const int r = f(j);
  87.                                 mf(&x, r);
  88.                                 a += x.v;
  89.                         }
  90.  
  91.                         b += a;
  92.                         x.v = 0;
  93.                 }
  94.  
  95.                 curtime = timeGetTime() - curtime;
  96.  
  97.                 cout << "boost : b = " << b << ", [" << curtime << " ms]" << endl << endl;
  98.         }
  99.  
  100.  
  101.         system("pause");
  102.  
  103. }
  104.  
  105. #include <loki/smallobj.cpp>
  106.  


i'd be appreciated if you find anything wrong with the code.
and i would also like to know your opinion about functors in a game, loki or boost, or maybe your have wrote your own functors :)

thanks

 
Christian Sigg

March 26, 2005, 09:59 AM

Hi Pet,

I'm using boosts functions because they work nicely with signals from the same library. I think they make a quite flexible team. Personally, I would never try to implement something myself that is already available in stl or boost. Of course, there is always an optimized costom solutions, but it is seldom worth the effort. For example, a fixed allocator once gained me seven fold speed up compared to stl allocator for hashmaps. But precisely, this was not even a rewrite, but an orthogonal specialization.
Therefore, my opinion is: use your functors, they are fun as long as you don't wrap your per-triangle-function-call with it. ;-)
Once you manage a team of a dozen slave programmers, ask someone to rewrite the functors and message passing system.

Christian

 
This thread contains 2 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.