fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <map>
  4.  
  5. class Controller
  6. {
  7. public:
  8. int function1(int a, int b) {return a + b;}
  9. int function2(int a, int b) {return a + b;}
  10. int function3(int a, int b) {return a * b;}
  11. int function4(int a, int b) {return a + 50 + b/2;}
  12. int function5(int a, int b) {return a + b;}
  13. int function6(int a, int b) {return a / 2 + b;}
  14. int function7(int a, int b) {return a + b * 19;}
  15. int function8(int a, int b) {return a + b * 20;}
  16. int function9(int a, int b) {return a + b + 100;}
  17. int function10(int a, int b) {return a / b;}
  18. };
  19.  
  20. typedef int (Controller::*function)(int a, int b);
  21.  
  22. class Dispatcher
  23. {
  24. public:
  25. Dispatcher()
  26. {
  27. m_functionsTable.insert(std::pair<std::string, function>("function1", &Controller::function1));
  28. m_functionsTable.insert(std::pair<std::string, function>("function2", &Controller::function2));
  29. m_functionsTable.insert(std::pair<std::string, function>("function3", &Controller::function3));
  30. m_functionsTable.insert(std::pair<std::string, function>("function4", &Controller::function4));
  31. m_functionsTable.insert(std::pair<std::string, function>("function5", &Controller::function5));
  32. m_functionsTable.insert(std::pair<std::string, function>("function6", &Controller::function6));
  33. m_functionsTable.insert(std::pair<std::string, function>("function7", &Controller::function7));
  34. m_functionsTable.insert(std::pair<std::string, function>("function8", &Controller::function8));
  35. m_functionsTable.insert(std::pair<std::string, function>("function9", &Controller::function9));
  36. m_functionsTable.insert(std::pair<std::string, function>("function10", &Controller::function10));
  37. }
  38. int FunctionDispatcher(std::string functionName, int a, int b) {
  39. Controller c;
  40. function f = m_functionsTable.find(functionName)->second;
  41. return (c.*(function)f)(a, b);
  42. }
  43. private:
  44. std::map<std::string, function> m_functionsTable;
  45. };
  46.  
  47.  
  48. int main()
  49. {
  50.  
  51. time_t t1 = time(NULL);
  52.  
  53. Dispatcher d;
  54. for (int i = 0; i < 100000000; i ++) {
  55. d.FunctionDispatcher("function1", i, i + 1);
  56. d.FunctionDispatcher("function2", i, i + 1);
  57. d.FunctionDispatcher("function3", i, i + 1);
  58. d.FunctionDispatcher("function4", i, i + 1);
  59. d.FunctionDispatcher("function5", i, i + 1);
  60. d.FunctionDispatcher("function6", i, i + 1);
  61. d.FunctionDispatcher("function7", i, i + 1);
  62. d.FunctionDispatcher("function8", i, i + 1);
  63. d.FunctionDispatcher("function9", i, i + 1);
  64. d.FunctionDispatcher("function10", i, i + 1);
  65.  
  66. }
  67.  
  68. time_t t2 = time(NULL);
  69. std::cout << t2 - t1 << std::endl;
  70. }
  71.  
Time limit exceeded #stdin #stdout 5s 15248KB
stdin
Standard input is empty
stdout
Standard output is empty