Article View: chile.comp.programacion
Article #3222Re: DUDAS C++
From: Waldo
Date: Fri, 15 Apr 2005 04:32
Date: Fri, 15 Apr 2005 04:32
54 lines
2238 bytes
2238 bytes
jpnews69 wrote:
> 1. Como puedo tranformar un numero en formato hora. ej: 18,5 0 =
"6:30pm"?
double x = 18.5;
double h, m = modf(x, &h);
printf("%02d:%02d%cm\n",
int(h) % 12, int(m * 60), h >= 12 ? 'p' : 'a');
> 2. Como puedo medir el tiempo que demora un programa en ejecutarse?,
> es decir, cual es el comando para guardar la hora al inicio y la
> hora al final del prog. para con la diferencia obtener el tiempo
> que se demora en ejecutarse?
No hay forma "standard". En POSIX hay varias maneras de obtener la
hora. Posiblemente la mas portable es:
// #include stdio.h, unistd.h, sys/time.h
#define tic(t) gettimeofday(&(t), 0)
// Correcta solo si t < now!
double tac(const timeval& t)
{
timeval now;
gettimeofday(&now, 0);
if (now.tv_usec < t.tv_usec) {
now.tv_usec += 1000000 - t.tv_usec;
now.tv_sec--;
} else
now.tv_usec -= t.tv_usec;
now.tv_sec -= t.tv_sec;
return now.tv_sec + (double)now.tv_usec / 1000000.0;
}
int main()
{
timeval t;
tic(t);
sleep(5);
printf("Eso tomo %g segundos\n", tac(t));
}
pero hay otras funciones, como clock(), times(), ftime(), y hasta
time() si no te interesa mas que una resolucion de un segundo. Todas
esas son POSIX.
Salu2,
W.
Technical Details
Message-ID:
<1113539563.284030.146080@g14g2000cwa.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!g14g2000cwa.googlegroups.com!not-for-mailReferences:
<425e747f@x-privat.org>Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,4523ef30705d6abe X-Google-Attributes: gid1593cb,public X-Google-Language: SPANISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!g14g2000cwa.googlegroups.com!not-for-mail From: "Waldo" <fatwaldo@yahoo.com> Newsgroups: chile.comp.programacion Subject: Re: DUDAS C++ Date: 14 Apr 2005 21:32:43 -0700 Organization: http://groups.google.com Lines: 55 Message-ID: <1113539563.284030.146080@g14g2000cwa.googlegroups.com> References: <425e747f@x-privat.org> NNTP-Posting-Host: 67.86.77.109 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1113539573 16857 127.0.0.1 (15 Apr 2005 04:32:53 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 15 Apr 2005 04:32:53 +0000 (UTC) User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: g14g2000cwa.googlegroups.com; posting-host=67.86.77.109; posting-account=hVjo8QwAAAC1br25yrexfun-OkEnsf06