Ich möchte wissen, welche Zeilen von C-Code zu einem Programm hinzugefügt werden, so dass es mir die Gesamtzeit angibt, die das Programm zum Ausführen benötigt. Ich denke, es sollte eine Zählerinitialisierung nahe dem Anfang von main und einer nach dem Ende der Hauptfunktion geben. Ist der richtige Header clock.h
?Verstrichene Zeit zum Ausführen eines C-Programms
Vielen Dank ...
aktualisieren Ich habe eine Maschine Win Xp. Ergibt es nur clock()
am Anfang und ein weiteres clock()
am Ende des Programms? Dann kann ich den Zeitunterschied schätzen. Ja, du hast recht, es ist time.h
.
Hier ist mein Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <share.h>
#include <time.h>
void f(long double fb[], long double fA, long double fB);
int main() {
clock_t start, end;
start = clock();
const int ARRAY_SIZE = 11;
long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
int i;
long double A, B;
if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}
A = 0.5;
B = 2;
for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}
z[1] = 5;
f(z, A, B);
for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);
free(z);
z = NULL;
end = clock();
printf("Took %ld ticks\n", end-start);
printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
return 0;
}
void f(long double fb[], long double fA, long double fB) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
Einige Fehler mit MVS2008:
testim.c(16) : error C2143: syntax error : missing ';' before 'const' testim.c(18) :error C2143: syntax error : missing ';' before 'type' testim.c(20) :error C2143: syntax error : missing ';' before 'type' testim.c(21) :error C2143: syntax error : missing ';' before 'type' testim.c(23) :error C2065: 'z' : undeclared identifier testim.c(23) :warning C4047: '==' : 'int' differs in levels of indirection from 'void *' testim.c(28) : error C2065: 'A' : undeclared identifier testim.c(28) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
und es geht zu 28 Fehler. Beachten Sie, dass ich keine Fehler/Warnungen ohne Ihre Uhrcodes habe.
AKTUELLES: Ich habe leider keine gute Antwort hier erhalten. Aber nach einer Suche auf Google funktioniert der Code. Hier ist sie:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void f(long double fb[], long double fA);
int main() {
clock_t start = clock();
const int ARRAY_SIZE = 11;
long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
int i;
long double A;
if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}
A = 0.5;
for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}
z[1] = 5;
f(z, A);
for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);
free(z);
z = NULL;
printf("Took %f seconds\n", ((double)clock()-start)/CLOCKS_PER_SEC);
return 0;
}
void f(long double fb[], long double fA) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
Prost
-Update am 10. April: Hier ist eine bessere Lösung dank "JustJeff"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void f(long double fb[], long double fA);
const int ARRAY_SIZE = 11;
int main(void)
{
long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
int i;
long double A;
LARGE_INTEGER freq;
LARGE_INTEGER t0, tF, tDiff;
double elapsedTime;
double resolution;
if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t0);
// code to be timed goes HERE
{
A = 0.5;
for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}
z[1] = 5;
f(z, A);
for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);
free(z);
z = NULL;
}
QueryPerformanceCounter(&tF);
tDiff.QuadPart = tF.QuadPart - t0.QuadPart;
elapsedTime = tDiff.QuadPart/(double) freq.QuadPart;
resolution = 1.0/(double) freq.QuadPart;
printf("Your performance counter ticks %I64u times per second\n", freq.QuadPart);
printf("Resolution is %lf nanoseconds\n", resolution*1e9);
printf("Code under test took %lf sec\n", elapsedTime);
return 0;
}
void f(long double fb[], long double fA) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
Es funktioniert sowohl mit MVS2008 und mit Borland C++ BuilderX von 2003.
Sie können eine Antwort posten und akzeptieren, wenn Sie das Gefühl haben, dass Sie jetzt die beste Antwort selbst haben. Dies kann hilfreich für jemanden sein, der nach einer Antwort auf die jeweilige Frage sucht (und ich bin mir sicher, dass andere Leute ständig danach suchen). – Cam
Ich weiß nicht, ob ich die richtige Antwort habe. Aber es funktioniert. Ich freue mich immer über konstruktive Vorschläge, während ich noch lerne. Kann ich meine eigene Antwort akzeptieren (ein Häkchen setzen)? – yCalleecharan
Ja, Sie können. Und übrigens, ich will nicht unhöflich sein, aber wie hast du hier keine gute Antwort bekommen? Du hast ein paar nette Antworten, die funktionieren sollten. Wenn Sie Fehler bekommen, ist das Ihr eigener Code, nicht was die Beantworter Ihnen gegeben haben. Wenn Sie nicht glauben, dass die Antworten gut sind, sagen Sie uns bitte, wie wir sie verbessern können. – Javier