How do I time a process in c++? - c++

I'm working on a project where I'm to time the creation of fork() and pthread_create(). We're supposed to time how long it takes to do each of these tasks by using system calls to create a personalized timer class. For assistance, the professor has told us to check man -k time.
I'm new to both using system calls and using man pages for documentation, so I'm entirely lost. So far the code I'm trying to get working is this:
#include "Timer.h"
#include <iostream>
#include <signal.h>
#include <ctime>
#include <time.h>
using namespace std;
Timer::Timer() {
timer_t * tid;
timer_create(CLOCK_REALTIME, NULL, tid);
int time = timer_gettime(tid, 0);
cout<<time;
}
When compiling, eclipse tosses me these errors:
undefined reference to 'timer_create', line 20
undefined reference to 'timer_gettime', line 21
The internet has pointed me in the direction of including the -lrt library when compiling, but I can't find anything that says how to do that, suggesting that perhaps it's entirely wrong.
So, am I on the right path? If I am, how am I supposed to get this code working?
Edit:
I got the clock() based timer working. The downside is that we're timing fork(), which isn't registering as taking any time at all when timing it. Any ideas?

You can use this
time_t start,end;
start=clock();//predefined function in c
//after the user defined function does its work
end=clock();
t=(end-start)/CLOCKS_PER_SEC;

Related

Keep MATLAB engine open in C++ dll

I want to create a dll which keeps an instance of the MATLAB engine open for use by other scripts. That is, the goal is to not have to keep initialising and closing the MATLAB instance, which takes time.
For some background, I have a working C++ program which initialises a MATLAB engine and contains a load of functions to do various things in MATLAB. As some kind of minimum example, I have the three scripts below.
header.h
#pragma once
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <tuple>
#pragma comment (lib,"libmat.lib")
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libmex.lib")
#pragma comment (lib,"libeng.lib")
// Example function definition
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir);
MatlabFunctions.cpp
#include "header.h"
// Example function
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir){
std::u16string matlabCommand = matlab::engine::convertUTF8StringToUTF16String("cd '"+dir+"'");
matlabPtr->eval(matlabCommand);
}
Main.cpp
#include "header.h"
int main(){
using namespace matlab::engine;
matlab::data::ArrayFactory factory; // Create MATLAB data array factory
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine
// Do various functions
}
My goal is to get rid of Main.cpp and create a dll which keeps keeps the MATLAB engine open, allowing the functions in MatlabFunctions.cpp to be run without having to start the MATLAB engine each time.
Is this possible? And if so, how can it be done?
Some people suggested to use IPC here for the task to be done. Using something like COM is not cross-platform and, well, pretty outdated at the moment. As a modern and cross-platform solution, I'd use an actual server application (which will start MATLAB engine and wait for requests) with, for example, gRPC running. Here's tutorial, pretty good one.
Other than that, you can specify your own networking protocol to run your tasks on the server application without the need of IPC, but the gRPC solution works simply out of the box, no bloat coding included.

Need a simple comparison with time() to execute things

I'm using an Adafruit Feather HUZZAH ESP8266 controller for a wordclock, which is a birthday present and should also be a marriage proposal.
Last time I programmed was years ago in school, so I'm rather noobish to that.
My plan is to make a simple condition if the desired time is reached (its only once), then light up some LEDs (marry me and her name) in heartform.
To not be overwritten a minute later i think an infinite while loop would be best. Push Reset and the clock gets time again over NTP.
So I searched for correct unix timestamp and tried things like
#include <iostream>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
int main(void)
{
while (time()==1461337750)
or
time_t t;
t = time(NULL); //oder time(&t);
if (t==1461335820)
}
but it didn't show anything.
I don't know what is the right way and need help, please! I searched the Inet for hours but couldn't find a similar task or I thought it wasn't.

C++ Playing Sound Lag

For my program I am making pac-man. Every time the pac-man eats a dot I want to play the munching sound. I already know how to play the sound but it causes lag too much lag. The lag is about 0.5 seconds but overall it slows down my game a bunch. Any suggestions?
#include <iostream>
#include <windows.h>
#pragma comment (lib , "winmm.lib") // Used for sound
using namespace std;
int main()
{
PlaySound(TEXT("Sounds\\pacman chomp.wav"), NULL, SND_FILENAME|SND_ASYNC);
system("pause");
}
If you want real "twitch time" audio you should probably look at DirectSound, that's what it was created for. The waveOutXXX APIs are better than PlaySound, but DirectSound is better still.

Sleep() function windows 8 c++

I'm attempting to get the Sleep() function working on a Windows 8 operating system, with c++. So far I haven't found any examples specific to this.
Following from http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx i've included the header <Synchapi.h>, though i get "Identifier "Sleep" is undefined". I am able to find the functions SleepConditionVariableCS() and SleepConditionVariableSRW() from Synchapi.h, but not Sleep().
Has anyone managed to use Sleep() on a Windows 8 operating system or know why I am unable to find the function?
You shouldn't be including Synchapi.h directly. Include Windows.h instead.
#include <Windows.h>
int main()
{
Sleep(1000);
return 0;
}
Are you using C++11? If you are you can use:
#include <thread>
template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep,Period>& sleep_duration );
Update
The Sleep() function is only available for Desktop Apps. Are you building a formerly known as Metro app? Or an App Store app?
See this article for some work arounds:
http://blogs.msdn.com/b/win8devsupport/archive/2012/11/28/introduce-multi-thread-programming-in-windows-store-apps-part-ii.aspx

'sqlite3_api' was not declared in this scope

I've been learning sqlite3 programming in C++ for the first time and this error confounds me and my internet searching abilities.
Here is my code, as far as it gets before throwing an error.
#include <iostream>
#include <sqlite3ext.h>
using namespace std;
int main()
{
sqlite3 *database;
int check;
check = sqlite3_open("introdb3.db", &database); //error is here
}
I'm pretty sure that it has something to do with the libraries that are (or aren't) being linked, but I can't figure out how to make it go properly.
I'm on Ubuntu using code::blocks.
Thanks!!
Instead of
#include <sqlite3ext.h>
write
#include <sqlite3.h>
The sqlite3ext.h file is only needed if you are going to write an SQLite extension - a custom function, for example. For regular database access, use sqlite3.h.