Timing the runtime of a program via the Terminal - c++

I've written a C++ program of which i would like to time the length of time it takes to complete - is there some terminal command i could use?

You can use the "time" command available in most (maybe all) the linux distributions. It will print the time spent as system, as user, and the total time.
For example
bash-4.1$ time (sleep 1; sleep 1)
will output something like
real 0m2.020s
user 0m0.014s
sys 0m0.005s
As you can see with the parenthesis you can launch every command chain you wish.

It's called time in *nix

Iterate over the function several times (1000's probably) so you can get a large enough number. Then use time.h to create two variables of type time_t - one before execution, one after. Subtract the two and divide by the iterations.

Or Measure-Command in PowerShell.

I try to better explain :)
If you have compiled your code using g++, for example:
g++ -std=c++14 c++/dijkstra_shortest_reach_2.cpp -o dsq
In order to run it, you type:
./dsq
In order to run it with a file content as an input, you type:
./dsq < input07Dijkstra.txt
Now for the answer.
In order to get the duration of the program output to the screen, just type:
time(./dsq < input07Dijkstra.txt)
Or without an input:
time(./dsq)
For the first command my output is:
real 0m16.082s
user 0m15.968s
sys 0m0.089s
Hope it helps!

Related

strange behavior with system command in c++

I have written a code in that at some point calls an external executable using the system command.
Here is how I do this:
First remove the output files of the external code.
boost::filesystem::remove("Results/CVground.bin");
boost::filesystem::remove("Results/CVground.BUD");
then write input files for the external code. The code is too long to paste here. It just writes few ascii files.
Then run the model. To make things even worse as you can see I'm calling a windows executable under linux using wine.
std::string sim_command = "/opt/wine-stable/bin/wine ";
sim_command.append(cvd.simulationExe()).append(" CVsimAqua.in >/dev/null");
int sys = system(sim_command.c_str());
Essentially the command I'm calling under system is the following
int sys = system("/opt/wine-stable/bin/wine Simulation3.02.exe CVsimAqua.in >/dev/null");
UPDATE:
Based on the suggestion I modified the above command so that it prints to a file as such
int sys = system("/opt/wine-stable/bin/wine Simulation3.02.exe CVsimAqua.in > log.dat");
This is usually takes a couple of minutes to complete.
Then I'm doing a check that the output files actually exists as follows:
if (!boost::filesystem::exists("Results/CVground.bin")) {
std::cout << "\t\tSys output from rank " << rank << " is " << sys << std::endl;
fun.clear();
fun.push_back(10000000);
fun.push_back(10000000);
boost::filesystem::current_path(main_dir);
return;
}
I have found that in some cases I do get the following print in my log file. The rank number is different in each run.
Sys output from rank 78 is 32512
What I don't understand is that the output of system is positive, which I believe it means that the system command was successful. Is there any way to capture more information from the system command?
UPDATE
After I changed the from /dev/null to log.dat I realized that when the system fails it doesn't even create the log.dat file, however I always check with system(NULL) that system is available before calling the system.
I have found extremely difficult to debug this because these errors occur only when I run the code on the cluster.
Is it possible that the system command returns before the execution code finishes? I have seen a definite NO answer to that question but I was wondering if things are getting trickier since I'm calling via wine.
Thank you
The system function returns the exit code from the command you run. By convention, processes return 0 on success and non-zero on failure. What a non-zero exit code means, depends on the application, wine in this case. If may be easier to debug if you don't redirect its output to /dev/null
There are some specific return values for when the system call itself fails: -1 is a process could not be created, and 127 if a shell could not be executed. See man 3 system for the details on those cases.

Is it possible to see which lines were executed after a command-line app was run?

I am using MinGW (GCC) as a C++ compiler within my application. I have set it to redirect the output of its command line process to my app. Now, suppose I have the following simple C++ code:
int n = 5;
if (n == 6) cout << "YES";
else cout << "NO";
Is there a way to tell what line(s) of code were actually hit during execution of the application? Is there a command I can send to MinGW (GCC) process which, for the given example, would output 1 and 3, as those were the lines hit. And also, in case of a line inside a "for" loop, to tell how many times that statement was actually hit?
And, if not possible, what would be the best approach to having this information? Developing my own compiler or...? Thanks in advance
EDIT: Can someone provide a snippet of commands (in Windows) to be used in order to create a coverage-enabled GCC exe file?
"Is there a way to tell what line(s) of code were actually hit during execution of the application?"
Yes. It's an intrinsic GCC feature. You'll need to compile and link your code with the --coverage, -lgcov or -fprofile-arcs options set.
The gcov tool can be used to consolidate and interpret the actual informations gathered during program runs, that were instrumented with --coverage.
A very good tool to produce browsable consolidated and fairly visualized covearage information from gcov outputs is lcov.
Since you're using mingw you should be able to use gcov: https://gcc.gnu.org/onlinedocs/gcc/Gcov.html

Is there a way to get a Unix timestamp in Stata?

I searched online and found several sources that talk about converting Unix timestamps to various workable formats, but none that allow me to actually get such a timestamp from within Stata. As of now, I use variations on
local curr_date = c(current_date)
local curr_time = c(current_time)
to apply timestamps to logs, data sets, etc. but I'd like to just use the Unix timestamp in seconds, if possible.
Are you familiar with help datetime? My understanding is that the Unix time stamp would be something like
display %12.0g clock("`c(current_date)' `c(current_time)'", "DMY hms" )/1000 - clock("1 Jan 1970", "DMY" )/1000
which of course you can use in other circumstances as well. (I am not a C programmer, I am a Stata user, but I do understand that it is easier for most people on this site to write a snippet of C code that would go into the guts of Stata than to RTFM... which is admirable in its own ways from where I sit, of course.)
One way to achieve this is to write a simple plugin. Compile this code, in a file called unixtimestamp.c
#include "stplugin.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
STDLL stata_call(int argc, char *argv[])
{
time_t seconds;
seconds = time(NULL);
char buf[33];
sprintf(buf, "%d", seconds);
SF_display(buf);
return(0);
}
with stplugin.h and stplugin.c in the same directory using this command (for a Linux system):
gcc -O3 -shared -DSYSTEM=OPUNIX -fPIC stplugin.c unix_timestamp.c -o unixtimestamp.plugin
The guide to creating plugins uses this command:
gcc -shared -DSYSTEM=OPUNIX stplugin.c unixtimestamp.c -o unixtimestamp.plugin
but on some systems, this gives an error instructing you to use the -fPIC flag, which is why I include it in my command. Also, optimizations aren't really necessary for such a simple plugin, but I included them regardless.
unixtimestamp.plugin should be placed in the ado/personal/ directory. Run Stata's sysdir function to find its location. On my system, it's HOME/ado/personal/ so I copied the plugin there. Then, from Stata, load the plugin:
program unixtimestamp, plugin
If no error message is displayed, run the plugin with:
plugin call unixtimestamp
As with any Stata command, you can also use a macro to simplify this if you plan to use this command frequently:
local unixtime plugin call unixtimestamp
`unixtime'
I use the following to grab a date/time stamp:
di "DateTime: $S_DATE $S_TIME"
Updated:
You may use Unix shell commands directly inside Stata by prefixing them an exclamation mark. To echo Unix time try:
!date +%s
1344341160

C++ - system command

I have a C++ program which is mainly used for video processing. Inside the program, I am launching the system command in order to obtain pass the processed videos to some other binaries to postprocess them.
I have a while loop towards infinite and I am launching the system command inside the loop every time. The thing is that at a certain point I am receiving the -1 return code from the system command. What could be the reason for that?
Inside the system command I am just calling a binary file with the adequate parameters from the main project.
The system command which I want to execute is actually a shell file.
In this file I am extracting the main feature from the video and passing them through a SVM model from a 3D party library in order to obtain the the desired classification.
./LiveGestureKernel ./Video ./SvmVideo
./mat4libsvm31 -l SvmVideoLabels < SvmVideo > temp_test_file
./svm-predict temp_test_file svm_model temp_output_file
cat < temp_output_file
rm -f temp_*
After a certain number of iterations through the while loop, it just won't execute the script file and I cannot figure out the reason for this. Thanks!
If you get -1 from the call to system(), you should first examine the contents of errno - that will most likely tell you what your specific problem is.
The one thing to watch out for is that the return value from system is an implementation-defined one in the case where you pass it a non-NULL command, so it's possible that -1 may be coming from your actual executable.
Your best bet in that case is to print out (or otherwise log) the command being executed on failure (and possibly all the time), so that you can check what happens with the same arguments when you execute it directly from a command line or shell.

Pre-assign parameter in script

Actually I have trouble naming the title of this post. Because I don't know how to summarize my meaning in a professional way. But I'll explain my question as below:
I'm running a program written by C++, command is:
./VariationHunter_SC
Then it'll let you type in many parameters:
Please enter the minimum paired-end insert size:
Please enter the maximum paired-end insert size:
Please enter the pre-processing mapping prune probability:
Please enter the name of the input file:
Please enter the minimum support for a cluster:
Obviously I need to type in such parameters one by one to run the program; But I have thousands of such jobs, and need to pre-assign such parameters in script, and submit script to computer.
So how can I do that?
thx
Edit
so how can I make parameter-list?
Just like below?:
140
160
0
mrfast.vh
1
Seems the program cannot recognize these numbers, and distribute numbers..
This depends on how the program actually reads the data that you type in - it's likely that its reading stdin, so you could use separate files with the parameters and pass them in via redirection: ./VariationHunter_SC < parameter-file
It's also possible that the program will accept parameters on the command line, but there's no way of really knowing that (or how) except by whatever documentation the program might come with (or by reading the source code, if it's available and there is no other accurate docs).
Simply use the piping character to pipe the contents of a file to your program
example, in a windows command shell:
echo "asdf" | pause
This will pass "asdf" to the pause program. As a result, pause will print a "Press any key to continue" message, then imediately continue because it will receive the "asdf" string as a response.
So, overall, write or use a program that outputs the contents of your file. Call it, then pipe its output to the program that needs the input.
The unix cat command is such a command that writes the contents of a file to output, or to the input of another executable if you are piping the output.