DOMANDA [C++] Tempo di esecuzione e stampa su carta

  • Autore discussione Autore discussione MPG
  • Data d'inizio Data d'inizio
Pubblicità
Stato
Discussione chiusa ad ulteriori risposte.
Ho messo 200000 e il programma va in tilt.......
Dimmi pure gli altri passi. Comuqne io prima mettevo manualmente i valori dell'array, prima anche se mettevo in maniera esatta la conta del tempo prima e dopo il sort il tutto non cambiava nel senso che l'excution time era sempre in funzione del tempo che ci mettevo nel mettere i valori, per questo mi chiedo ancora il perchè se appunto mettevo la conta del tempo nei punti giusti..
Poi comunque quello 0 che appare a cosa di riferisce perchè giustamente vorrei capire..
Al tempo di sorting.
Metti 20000 1 secondo dovrebbe uscirti almeno.

Ora devi usare l’header chrono. Sopra ti ho postato quello che devi mettere. Questo tiene conto dei millisecondi (quindi non ci sarà più 0 ma 0.00-qualcosa).

Ah si aggiungi #include <chrono>
 
Con 20000 viene 1 ed execution time 0.918 sec., con 80000 15 ed execution time 152714 sec.
 
Ho messo 200000 e il programma va in tilt.......
Dimmi pure gli altri passi. Comuqne io prima mettevo manualmente i valori dell'array, prima anche se mettevo in maniera esatta la conta del tempo prima e dopo il sort il tutto non cambiava nel senso che l'execution time era sempre in funzione del tempo che ci mettevo nel mettere i valori, per questo mi chiedo ancora il perchè se appunto mettevo la conta del tempo nei punti giusti.. doveva venire anche prima il tempo dell'ordinamento e non quello di tutto il processo: perchè???
Poi comunque quello 0 che appare a cosa di riferisce perchè giustamente vorrei capire.. Ho messo 17500 il valore, mi ha dato 1 oltre all'execution time perchè?
Se per "va in tilt" intendi che crasha, potrebbe proprio essere uno stack overflow.

Inviato da ONEPLUS A5000 tramite App ufficiale di Tom\'s Hardware Italia Forum
 
SE metto
#include <chrono>
va tutto codeblock va in tilt o meglio compare quanto sotto:




// Copyright (C) 2007-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.

/** @file bits/c++0x_warning.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{iosfwd}
*/

#ifndef _CXX0X_WARNING_H
#define _CXX0X_WARNING_H 1

#if __cplusplus < 201103L
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif

#endif
 
SE metto
#include <chrono>
va tutto codeblock va in tilt o meglio compare quanto sotto:




// Copyright (C) 2007-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.

/** @file bits/c++0x_warning.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{iosfwd}
*/

#ifndef _CXX0X_WARNING_H
#define _CXX0X_WARNING_H 1

#if __cplusplus < 201103L
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif

#endif
È stato introdotto con C++11 quindi devi mettere il compilatore nello standard C++11. Se non sai come fare usa il metodo clock() che è stato scritto sopra.
Se per "va in tilt" intendi che crasha, potrebbe proprio essere uno stack overflow.

Inviato da ONEPLUS A5000 tramite App ufficiale di Tom\'s Hardware Italia Forum
Sarebbe da analizzare cosa significhi “tilt”. Magari resta il programma in funzione ma sembra non dare risposta...
 
Ok fatto con codeblocks aggiunto header chrono , ma per avere 0.00 come dicevi?
 
Ok fatto con codeblocks aggiunto header chrono , ma per avere 0.00 come dicevi?
Tutto il codice che ti ho scritto sopra. auto bla bla bla
puntoA va prima del while e puntoB dopo. Poi scrivi direttamente quel cout << duration_cast bla bla bla e hai finito
 
Fatto cosi'. Con 2000 viene 0 e sotto 0.00602 execution time 0.035.
In pratica 0 è il sort, piu' specificamente 0.00602 mentre 0.035 sec e ' il tempo di tutto il processo dell'OS, giusto?
Se volessi mettere a questo punto un ordinamento dei valori dell'array oltre ai tempi?

Codice:
#include <iostream>
#include <ctime>
#include<cstdlib>
#include <chrono>
using namespace std;
using namespace chrono;

int main()


{
    srand(time(NULL));



const int n = 2000;
int vet[n];
int tmp;
for(int i=0;i<n;i++)
{
    vet[i]=rand()%9;
}

cout<<endl;
bool flag =  true;
int stop = n - 1;

time_t inizio = time(NULL);
auto puntoA = high_resolution_clock::now();
while (flag)
{
    flag = false;
    for (int i = 0; i <= stop; i++)
        if (vet[i] > vet[i + 1])
        {

            tmp=vet[i];
            vet[i]=vet[i+1];
            vet[i+1]=tmp;
            flag = true;
        }

    stop = stop - 1;
}
double tempoDiSorting = difftime(time(NULL), inizio);


{
      cout<<tempoDiSorting<<endl;
      auto puntoB = high_resolution_clock::now();

cout << "tempo del sort :   "<<duration_cast<duration<double>>(puntoB - puntoA).count();


}
}
 
Fatto cosi'. Con 2000 viene 0 e sotto 0.00602 execution time 0.035.
In pratica 0 è il sort, piu' specificamente 0.00602 mentre 0.035 sec e ' il tempo di tutto il processo dell'OS, giusto?
Se volessi mettere a questo punto un ordinamento dei valori dell'array oltre ai tempi?

Codice:
#include <iostream>
#include <ctime>
#include<cstdlib>
#include <chrono>
using namespace std;
using namespace chrono;

int main()


{
    srand(time(NULL));



const int n = 2000;
int vet[n];
int tmp;
for(int i=0;i<n;i++)
{
    vet[i]=rand()%9;
}

cout<<endl;
bool flag =  true;
int stop = n - 1;

time_t inizio = time(NULL);
auto puntoA = high_resolution_clock::now();
while (flag)
{
    flag = false;
    for (int i = 0; i <= stop; i++)
        if (vet[i] > vet[i + 1])
        {

            tmp=vet[i];
            vet[i]=vet[i+1];
            vet[i+1]=tmp;
            flag = true;
        }

    stop = stop - 1;
}
double tempoDiSorting = difftime(time(NULL), inizio);


{
      cout<<tempoDiSorting<<endl;
      auto puntoB = high_resolution_clock::now();

cout << "tempo del sort :   "<<duration_cast<duration<double>>(puntoB - puntoA).count();


}
}
Non mettere alcun stream prima di dare il valore al puntoB. La scrittura a video prende un sacco di tempo.
Cava tutto ciò legato a time(), ormai è inutile.
 
Ok . Ora se volessi anche stampare a video l'ordinamento array? (metterei un array di piccole dimensioni) perchè devo fare vedere a video che il sort va bene...
 
Ok . Ora se volessi anche stampare a video l'ordinamento array? (metterei un array di piccole dimensioni) perchè devo fare vedere a video che il sort va bene...
Fallo tranquillamente, ma dopo aver dato il valore a puntoB
 
Facendo cosi' pero' poi non mi viene bene il time sort, viene sempre 00 o 0-20004555 scritto cosi...:

Codice:
#include <iostream>
#include <ctime>
#include<cstdlib>
#include <chrono>
using namespace std;
using namespace chrono;

int main()


{
    srand(time(NULL));



const int n = 10;
int vet[n];
int tmp;
for(int i=0;i<n;i++)
{
    vet[i]=rand()%9;
}

cout<<endl;
bool flag =  true;
int stop = n - 1;

auto puntoA = high_resolution_clock::now();
while (flag)
{
    flag = false;
    for (int i = 0; i <= stop; i++)
        if (vet[i] > vet[i + 1])
        {

            tmp=vet[i];
            vet[i]=vet[i+1];
            vet[i+1]=tmp;
            flag = true;
        }

    stop = stop - 1;
}


{
      auto puntoB = high_resolution_clock::now();

cout << "tempo del sort :   "<<duration_cast<duration<double>>(puntoB - puntoA).count();

}
for (int i = 0; i < n; i++)
cout<<vet[i]<<endl;

}
 
Facendo cosi' pero' poi non mi viene bene il time sort, viene sempre 00 o 0-20004555 scritto cosi...:

Codice:
#include <iostream>
#include <ctime>
#include<cstdlib>
#include <chrono>
using namespace std;
using namespace chrono;

int main()


{
    srand(time(NULL));



const int n = 10;
int vet[n];
int tmp;
for(int i=0;i<n;i++)
{
    vet[i]=rand()%9;
}

cout<<endl;
bool flag =  true;
int stop = n - 1;

auto puntoA = high_resolution_clock::now();
while (flag)
{
    flag = false;
    for (int i = 0; i <= stop; i++)
        if (vet[i] > vet[i + 1])
        {

            tmp=vet[i];
            vet[i]=vet[i+1];
            vet[i+1]=tmp;
            flag = true;
        }

    stop = stop - 1;
}


{
      auto puntoB = high_resolution_clock::now();

cout << "tempo del sort :   "<<duration_cast<duration<double>>(puntoB - puntoA).count();

}
for (int i = 0; i < n; i++)
cout<<vet[i]<<endl;

}
Beh 00 ti viene perché manca un endl. 0 è del tempo e l’altro 0 è il primo elemento. Intanto togli pure quelle graffe inutili che hai lasciato.
Comunque non dovrebbe nemmeno succedere, visto che stai usando l’orologio ad alta precisione. Se continua al posto di duration<double> scrivi nanoseconds. Il risultato sarà grande (perché in nanosecondi).
 
Beh 00 ti viene perché manca un endl. 0 è del tempo e l’altro 0 è il primo elemento. Intanto togli pure quelle graffe inutili che hai lasciato.
Comunque non dovrebbe nemmeno succedere, visto che stai usando l’orologio ad alta precisione. Se continua al posto di duration<double> scrivi nanoseconds. Il risultato sarà grande (perché in nanosecondi).

Con nanoseconds non va... INoltre ho dovuto rimettere i vari time con il Tempodisorting perchè dopo che li ho tolti il time sort mi da' sempre 0... anche se ora sto usando tutto e continua a venire 0 il tempo di sorting e anche la differenza punto B e puntoA, non capisco...
 
Ultima modifica:
Con nanoseconds non va... INoltre ho dovuto rimettere i vari time con il Tempodisorting perchè dopo che li ho tolti il time sort mi da' sempre 0... anche se ora sto usando tutto e continua a venire 0 il tempo di sorting e anche la differenza punto B e puntoA, non capisco...
Questo funziona perfettamente. Hai evidentemente sbagliato qualcosa
C++:
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <chrono>

using namespace std;
using namespace chrono;

int main()
{
    const int n = 100000;
    int array[n];
    srand(time(NULL));

    for (int i = 0; i < n; i++)
        array[i] = rand() % 100;

    auto inizio = high_resolution_clock::now();

    sort(array, array + n); // std::sort(), già pronto all'uso

    auto fine = high_resolution_clock::now();

    cout << duration_cast<duration<double>>(fine - inizio).count() << endl;

    system("pause");

}
 
Ultima modifica:
Stato
Discussione chiusa ad ulteriori risposte.
Pubblicità
Pubblicità
Indietro
Top