0

So i made a c++ program to draw a line with DDA algorithm, i want to fins how long fast the program finish, but it's seems that the clock function i put can not be printed out

#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <time.h>

using namespace std;

int opt, x1 = 50, y1 =50, x2 = 50, y2=200;
int xt, yt;
int dx;
int dy;
int i;
int j;
int up;

int gd = DETECT, gm;

int main()
{
    clock_t exe, exe2;
    exe = clock();

    dx=(x2-x1);
    dy=(y2-y1);

    if(dx > dy)
        up=dx;
    else
        up=dy;

    float xi=(x2-x1)/up;
    float yi=(y2-y1)/up;

    int x=x1;
    int y=y1;
    int xt=x1;
    int yt=y1;

    initgraph(&gd, &gm, "C:\\TC\\BGI");

    putpixel(x,y, 15);

    for(j=1;j<=500;j++)
    {
        for(i=1;i<=up;i++)
        {

            x += xi;
            y += yi;
            putpixel(abs(x),abs(y), 15);

        }
        x = xt;
        y=yt;
    }
    getch();
    closegraph();
    exe2 = clock();
    float diff = ((float)exe2 - (float)exe);
    cout << "The time: " << diff << endl;
    system("pause");
    return 0;
}

the line is showed up in the graphic window, but after i close the graphic window, the program just stop without showing the result from diff

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    I removed the code that relies on external libraries, defined the global variables locally (I don't see why you need them global), and compiled the code using VC2015. The resulting program correctly displays the processor time used. Is this really the exact code you used? Concerning the use of graphics.h, you might also want to have a look at [this question](http://stackoverflow.com/questions/7860569/how-i-can-get-and-use-the-header-file-graphics-h-in-my-c-program) – Paul R. Apr 15 '16 at 10:32
  • 4
    I started counting the antiquated non-standard 1980s DOS nonsense in this program but eventually gave up. – Lightness Races in Orbit Apr 15 '16 at 10:51

3 Answers3

0

Try in the following way.
If the execution do not pause at system("pause"); try to read something from keyboard like cin >> x.

#include <time.h>
#include <stdio.h>

time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf.", dif );
0

There is a lot of ways too achieve that (profiling). But it seems that you want a basic method:

std::cerr << "Diff : " << diff << std::endl;

I'm not sure because I'm not on Window, but I think all cerr messages will stay inside the Visual Studio console.

A little more generic way to keep a trace of GUI application is a log system. In the simplest form : write what you want into a log file(text file). But for the whole application time you'll have some extra time to open the file.


PS : If someday you attempt to do some profiling on a complex program : wiki

Mathieu Van Nevel
  • 1,428
  • 1
  • 10
  • 26
0

Or you could direct the stdout output to a file:

freopen("myfile.log", "w", stdout);

Using profilers can also determine the speed of your programs.