Efficiency issue

closed account (SECMoG1T)
Hi av been workin on this http://www.cplusplus.com/forum/beginner/149886/ program from yesterday and am having a couple of logical bugs and design fawls and i need your advice.

For example:
The program only works well only if i use a loop that simulates monitoring for less than fifty times, for larger simulation about 1,000,000 the program is totally impractical and the results are constatnt from about the 20th iteration..... please check it out in main() and help me improve its efficiency and perfomance or even it's coding.

Any advice on how to improve it will be gladly appreciated.

employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>

/***this is a basic class,it's objects can carry a variety of office tasks,**/
/***used to intialize employee tasks****/
struct Task
{
    Task():mytask(""){}
    Task(std::string new_task):mytask(new_task){};
    std::string mytask;
    bool done=false;
};

/**************Employee class*********************/
class employee
{
  private:
      std::string name;
      std::string message{""};
      std::vector<Task> mytasks{0};
      std::size_t failed_tasks;
      bool warned=false;
      bool tobe_fired=false;
      bool fired=false;
      int days;
      bool check_failing();
      void reset_tasks();/// new funct
  public:
      /***------------used constructor-----------------------------------***/
      employee(const std::string& nm,const std::vector<Task> vec)
       :name(nm),failed_tasks(0),mytasks(vec.begin(),vec.end()),days(0){}

      employee ():name(""),failed_tasks(0),days(0){}

      /***-------------getters--------------------------------------------***/
      bool to_be_fired() const{ return tobe_fired;}
      bool got_fired()     const {return fired;}
      int    getdays()      const{  return days;}
      bool blacklist()       const{return(warned&&days>=3);}
      std::string getname() const {return name;}

      /***-------------setters---------------------------------------------***/
      void work();
      void setfired();
      void receive_mssg(const std::string mssg);
};

///@param string, @brief receives a message sent from mananger
///it also sets the warned variable to true, this variable is
/// used by blacklist member function
void employee::receive_mssg(const std::string mssg)
 {
      if(warned){}
      else
      {
        message=mssg;
        warned=true;
        days=1;
      }
 }


///@param void, @brief will be called by check_failling
/// after the failing test is over to reset all tsks for the next evaluation;
 void employee::reset_tasks()
 {
     for(auto &i: mytasks)
     {
       i.done=false;
     }
 }

///@param void, @brief checks if an employees failed to accomplish more than 2
/// tasks, each employee will have atleast six tasks
bool employee::check_failing()
{
   int hrs_to_wrk=(rand()%6)+3;///each emp must always accomplish more than 3 tasks

    if(got_fired())
        return false;

   for(unsigned int i=0;i<mytasks.size()&&i<hrs_to_wrk; i++)
      {
          mytasks[i].done=true;
      }

    for(const auto tsk: mytasks)
    {
        if(!tsk.done)
            ++failed_tasks;
    }

    reset_tasks();  ///this clears all task to default

    if(failed_tasks>=2)
           return true;
    else
           return false;
}

///@param void, @brief simulates work progress, each day failed is counted, if
///failed days accumlate to five, the employee should be fired by the mananger
void employee::work()
{
  if(!got_fired())
  {    if(days>=5)
       {
           tobe_fired=true;
       }
       else
       {
        if(check_failing())
            {++days; }
       }
  }
}

///@param void, sets the "fired" variable to true, function is only used
/// by mananger to fire employees
void employee::setfired()
{
    fired=true;
}
#endif // EMPLOYEE_H 



manager.h


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

#ifndef MANAGER_H
#define MANAGER_H

#include "employee.h"

/*******Manager Class***************/
class manager
{
private:
    std::string name;
    double salary;
    std::vector<employee> mylist{0};
    void evaluate();
    void send_mssg(const std::string& message,employee& emp);
public:
    manager(const std::string& nm,double amnt,const std::vector<employee> vec)
     :name(nm),salary(amnt),mylist(vec.begin(),vec.end()){}
    void monitor();
    void show() const;
};

///@param string massage, employee object
///@brief the message is sent to the employee obbject via the employee
///member receive_mssg() function, send_mssg() is a private member
void manager::send_mssg(const std::string& message,employee& emp)
{
    emp.receive_mssg(message);
}

///@param void
///@brief private member function, used by the public function
///monitor to evaluate an employee working progress, used to fire empployees
void manager::evaluate()
{
  for(auto& wrk: mylist)
  {
      wrk.work();
  }

  for(auto& i: mylist)
  {
     if(i.got_fired()){}

     else if(i.to_be_fired())
     {
         i.setfired();
        // std::cout<<" \t fired"<<std::endl;
     }

     else if(i.getdays()>2)
     {
         send_mssg(std::string("You have failed for two days"),i);
     }
  }
}

///@param void, @brief simply calls evalute();
void manager::monitor()
{
  if(mylist.empty())
    std::cout<<"No employee to be monitored\n";
  else
  {
    evaluate();
  }
}

///@param void, @brief prints the progress results
void manager::show() const
{
    std::cout<<"\n\n";
    for(int j=0;j<30;j++)
       std::cout<<"--";
  std::cout<<"\nManager          : "<<name<<"  Salary:     "<<salary<<std::endl;
  std::cout<<"Current active employees\n";

  size_t count=1;
  for(auto my_emply: mylist)
  {
     if(!my_emply.got_fired()&&!my_emply.to_be_fired())
     {
        std::cout<<"\tNO "<<count<<" "<<my_emply.getname()<<"\tDays failed "<<my_emply.getdays()<<std::endl;
        ++count;
     }
  }

   std::cout<<"\n\nBlacklisted emoployees\n";
   count=1;
   for(auto my_emp: mylist)
   {
      if(!my_emp.to_be_fired()&&!my_emp.got_fired()&&my_emp.blacklist())
      {
        std::cout<<"\tNO "<<count<<" "<<my_emp.getname()<<"\tDays failed"<<my_emp.getdays()<<std::endl;
        ++count;
      }
   }

   std::cout<<"\n\nFired employees\n";
   count=1;
   for(auto emp: mylist)
   {
     if(emp.got_fired())
     {
       std::cout<<"\tNO "<<count<<" "<<emp.getname()<<"\tDays failed "<<emp.getdays()<<std::endl;
       ++count;
     }
   }
}
#endif // MANAGER_H 



main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "manager.h"
#include <ctime>

int main()
{
 std::cout.sync_with_stdio(false);/// optimizer. new
 srand(time(nullptr));

  std::vector<Task> To_do{std::string("Accounting"),std::string("Customer care"),
                          std::string("Drafting")  ,std::string("IT support"),
                          std::string("Medical support"),std::string("Maintainance"),
                          std::string("Cleaning")  ,std::string("Write press")
                          };

  std::vector<employee> my_employees{employee("John",To_do),employee("Kylie",To_do),
                                     employee("Jeson",To_do),employee("Bobby",To_do),
                                     employee("Ashley",To_do),employee("kinsly",To_do),
                                     employee("labrinth",To_do),employee("medley",To_do),
                                     employee("Madison",To_do),employee("Mia",To_do),
                                     employee("Sydney",To_do),employee("Bailey",To_do)
                                     };


  manager major("BIGMAN",4500.50,my_employees);


  for(int i=0;i<1000000;i++)
  {
      major.monitor();
      /// major.show();
  }

        major.show();

}


please check it out and point out where i should improve, Thank you very much.
Last edited on
closed account (SECMoG1T)
Anybody wanna give it a trial? i'll appreciate.
Last edited on
Main problem: as soon as employee completes task, it stays complete. This meants that those who were lucky enough to complete enough tasks before they are fired will continue to work forever.

Alo your main problem is output. Console is slow. Turn on optimisation, comment out show() routine in the loop and add a single one after the loop. Even one million iterations will be calculated in less than second.
Turnin off C and C++ stream syncronisation might help a little: std::cout.sync_with_stdio(false); but for large amount of output you want to limit how much is displayed. You can save the rest into file.
closed account (SECMoG1T)
@Miinipaa thank you very much, completes task, it stays complete. yea i dint think of this maybe i can reset them after some iterations, that's a great idea, amaizing. i will laugh at myself haha.

Gr8 advice but where will i get this... std::cout.sync_with_stdio(false); or where will i put it. Also where will turn on this Turn on optimisation

thank you very much.
Last edited on
but where will i get this
At the very beginning of main. Note that after this you should not mix C (printf/scanf/gets) and C++ streams (cin/cout)
closed account (SECMoG1T)
Thank you very much @Minnipaa am working on it right now.
closed account (SECMoG1T)
It worked yeah, hurray! yeaaaa. i actually increased the count in main
for(int i=0;i<1000000;i++) to 10,000,000 and it worked in 8s
Process terminated with status 0 (0 minute(s), 8 second(s)) this is awesome haha, it used to take hrs.

i also added this code and all my bugs are gone ne'o. hehe yea.
1
2
3
4
5
6
7
8
9
10
11
12
///@param void, @brief will be called by check_failling
/// after the failing test is over to reset all tsks for the next evaluation;
 void employee::reset_tasks()
 {
     for(auto &i: mytasks)
     {
       i.done=false;
     }
 }

///this too was added in main
std::cout.sync_with_stdio(false);



the first post is updated now, it's all working greeeeeaaat.
Thank you very very much @Minnipaa
Last edited on
Topic archived. No new replies allowed.