Testframe für Codeschnippsel



  • hi,

    Habe mir ein Testframe gebastelt, womit ich die Zeiten für einzelne Codeteile messen kann. Das Problem ist, dass wenn die verflossene Zeit unter einer Sekunde ist nur Null rauskommt. Muss an der Division liegen, find den Fehler aber nicht.
    Hier mal der Code:

    #include <iostream>
    #include <windows.h>
    
    void welcome()
    {
    	std::cout << "Welcome!\npress enter to begin" << std::endl;
    	std::cin.get();
    }
    
    void end()
    {
    	std::cout << "press enter to leave\nBye!" << std::endl;
    	std::cin.clear();
    	std::cin.ignore( std::cin.rdbuf()->in_avail() );
    	std::cin.get();
    }
    
    #define PERFORMANCETIMER
    
    int main()
    {
    	welcome();
    #ifdef PERFORMANCETIMER
    	unsigned __int64 frequency;
    	unsigned __int64 start_ticks;
        unsigned __int64 current_ticks;
    	int cycles = 1;
    	QueryPerformanceFrequency( (LARGE_INTEGER*)&frequency );   // <--- Das gecaste stört mich auch
    	QueryPerformanceCounter( (LARGE_INTEGER*)&start_ticks );
    	for( int i = 0; i < cycles; ++i )
    #endif
    	// Testblock
    	{
    		Sleep( 500 );
    	}
    	// Testblock
    #ifdef PERFORMANCETIMER
    	QueryPerformanceCounter( (LARGE_INTEGER*)&current_ticks );
    	unsigned int passed_ticks = ( current_ticks - start_ticks );
    	float passed_seconds = passed_ticks / frequency;      // <--- Wenn dies kleiner als 1 ist, kommt 0 raus ???
    	unsigned int ticks_per_cycle = passed_ticks / cycles;
    	float milliseconds_per_cycle = ( passed_seconds / cycles ) * 1000;  // <--- Hier auch :(
    	std::cout << "Results:\nProcessed " << cycles << " cycles in " 
    		<< passed_seconds << " seconds\nPer cycle passed " << ticks_per_cycle
    		<< " ticks, that are " << milliseconds_per_cycle << " milliseconds\n"
    		<< std::endl;
    #endif
    	end();
    }
    

    Compiler schrieb:

    main.cpp(39) : warning C4244: 'initializing' : conversion from 'unsigned __int64' to 'unsigned int', possible loss of data
    main.cpp(40) : warning C4244: 'initializing' : conversion from 'unsigned __int64' to 'float', possible loss of data

    Hoffe ihr könnt mir Helfen.
    Wenn ihr anderes zu bemängeln habt, nur zu 😉

    Danke im voraus
    Grüsse
    PJ



  • Hallo

    bekanntes Problem : Wenn zu zwei Integer-Variablen dividierst, kommt als Ergebnis erstmal ein gerundeter Integer-Wert raus, der dann als float interpretiert wird.
    Du must also gleich mit floats dividieren

    float passed_seconds = float(passed_ticks) / float(frequency);
    

    bis bald
    akari



  • Danke, danke.
    Ich habs nicht mehr gesehn..

    bye PJ


Anmelden zum Antworten