Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    420

    [c/c++] questo codice è C o C++??

    Salve a tutti, sapreste dirmi se questo codice sia C o C++??
    E spiegarmi brevemente che cosa fa?? (mi fareste un grossissimo piacere)
    ciao

    #include <stdio.h>
    #include <time.h>
    #ifdef _WIN32
    #include <windows.h>

    static LARGE_INTEGER _tstart, _tend;
    static LARGE_INTEGER freq;

    void tstart(void)
    {
    static int first = 1;
    if(first){
    QueryPerformanceFrequency(&freq);
    first = 0;
    }
    QueryPerformanceCounter(&_tstart);
    }

    void tend(void)
    {
    QueryPerformanceCounter(&_tend);
    }

    double tval()
    {
    return ((double)_tend.QuadPart - (double)_tstart.QuadPart)/((double)freq.QuadPart);
    }
    #else
    #include <sys/time.h>

    static struct timeval _tstart, _tend;
    static struct timezone tz;

    void tstart(void)
    {
    gettimeofday(&_tstart, &tz);
    }

    void tend(void)
    {
    gettimeofday(&_tend,&tz);
    }

    double tval()
    {
    typedef signed long s;
    s t1, t2;
    t1 = (s)_tstart.tv_sec + (s)_tstart.tv_usec/(1000*1000);
    t2 = (s)_tend.tv_sec + (s)_tend.tv_usec/(1000*1000);
    return t2-t1;
    }
    #endif

    main(){

    tstart();
    funzionequalsiasi();
    tend();
    double tempo=tval();
    printf("\n tempo di calcolo : %f", tempo);
    }
    the sALIEN

  2. #2
    Utente di HTML.it L'avatar di Brainjar
    Registrato dal
    Nov 2004
    Messaggi
    1,162
    La sintassi è tipica del C, non ci sono classi e/o oggetti e l'uso della printf() per la scittura a console è del C.

    Il programmino permette di calcolare il tempo utilizzato dalla
    elaborazione della funzione funzionequalsiasi(), che tra parentesi
    non è ne dichiarata, ne prototipizzata.

    E' quindi un programmino generico di valutazione tempi di esecuzione.

    Ciao,
    Ciao, Brainjar

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    420
    Ok, se è in C, che cosa sono allora questi QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&_tstart);
    (s)_tstart.tv_sec
    e soprattutto, cosa fanno???
    the sALIEN

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    420
    #ifdef _WIN32 poi, mi sapreste dire dove verrebbe già definito _WIN32? in #include <stdio.h> , in #include <time.h> o viene definito dal compilatore in qualche altra strana maniera?
    the sALIEN

  5. #5
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    In genere viene definito automaticamente dal compilatore

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    420
    ok,

    e questa parte:

    #else
    #include <sys/time.h>

    static struct timeval _tstart, _tend;
    static struct timezone tz;

    void tstart(void)
    {
    gettimeofday(&_tstart, &tz);
    }

    void tend(void)
    {
    gettimeofday(&_tend,&tz);
    }

    double tval()
    {
    typedef signed long s;
    s t1, t2;
    t1 = (s)_tstart.tv_sec + (s)_tstart.tv_usec/(1000*1000);
    t2 = (s)_tend.tv_sec + (s)_tend.tv_usec/(1000*1000);
    return t2-t1;
    }
    #endif


    è la parte opzionale che viene implementata se il sistema operativo non è windows vero?
    e quindi sys/time.h dovrebbe essere una libreria di linux, giusto?
    ma se fosse così perchè per windows, tstart e tend sono
    LARGE_INTEGER cioè longlong (64 bit) mentre per linux sono unsigned long 32 bit?
    the sALIEN

  7. #7
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    perche linux e windows hanno funzioni diverse per misurare i tempi, diversi nel nome, nel tipo di ritorno ed eventualmente nel tipo dei parametri..la libreira standard del C è abbastanza minimale, quasi tutte le funzioni necessarie per interagire col sistema sono diverse da un sistema operativo all'altro

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

  8. #8
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    420
    ok, grazie 1000
    the sALIEN

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.