Skip to content

Instantly share code, notes, and snippets.

@guigaoliveira
Created March 16, 2020 13:51
Show Gist options
  • Save guigaoliveira/fc60bccb25abfb024ce713258bfeb33c to your computer and use it in GitHub Desktop.
Save guigaoliveira/fc60bccb25abfb024ce713258bfeb33c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
using namespace std;
// função para simular carregamento da CPU
void carga(int k)
{
float f = 0.999999;
for(int i=0; i<k; i++)
{
f = f*f*f*f*f;
f = 1.56;
for(int j=0; j<k; j++)
{
f = sin(f)*sin(f)*f*f*f;
}
}
}
int main (void){
struct timeval tiC, tfC, tiE, tfE;
int tempoE, tempoC, r=0;
while(1){
//cout << "r: " << r << endl;
//cout << "TempoC: " << tempoC << "TempoE: " << tempoE << endl;
// Calcula o tempo de uso do processador
gettimeofday(&tiC, NULL);
carga(r);
gettimeofday(&tfC, NULL);
tempoC = 1000*(tfC.tv_sec-tiC.tv_sec) + (tfC.tv_usec-tiC.tv_usec);
// Calcula o tempo de espera
gettimeofday(&tiE, NULL);
usleep(5000);
gettimeofday(&tfE, NULL);
tempoE = 1000*(tfE.tv_sec-tiE.tv_sec) +(tfE.tv_usec-tiE.tv_usec);
/// Manipula a variavel de tempo de uso do processador
if(tempoC<tempoE){
r++;
}
if(tempoC>tempoE){
r--;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment