www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/04/06:53:34

From: gilleta AT lim DOT univ-mrs DOT fr (Bruno Gilleta)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Theoretical question of loops
Date: 4 Feb 1997 11:57:43 GMT
Organization: UNIVERSITE MARSEILLE
Lines: 82
Message-ID: <5d787n$8ne@newsup.univ-mrs.fr>
References: <32F443AF DOT 51CF AT dmv DOT com>
NNTP-Posting-Host: pento.esil.univ-mrs.fr
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Pyro Technic <invid AT dmv DOT com> wrote:
>I've got a weird question, wich would be faster and how would I
>measuree it. Would doing 2 things 3 times be faster or slower than doing
>3 things 2 times.

Hi!

Here is some code to mesure speed :


#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>

void
stuff_here(void);

struct rusage       t1, t2;

double
cputime()
{
    getrusage(RUSAGE_SELF, &t2);
    return (t2.ru_utime.tv_sec - t1.ru_utime.tv_sec)
      + (t2.ru_utime.tv_usec - t1.ru_utime.tv_usec) / 1.0e6;
}

void
initcputime()
{
    getrusage(RUSAGE_SELF, &t1);
}

int
main()
{
    int                 i, j, k;

    initcputime();
    for (k = 0; k < 10000000; k++)
	for (i = 0; i < 3; ++i) {
	    for (j = 0; j < 2; ++j) {
		stuff_here();
	    }
	}
    printf("loop set 1 : %f s.\n", cputime());

    initcputime();
    for (k = 0; k < 10000000; k++)
	for (i = 0; i < 2; ++i) {
	    for (j = 0; j < 3; ++j) {
		stuff_here();
	    }
	}
    printf("loop set 2 : %f s.\n", cputime());

    return 0;
}



The answers :

without optimisation :
loop set 1 : 20.419021 s.
loop set 2 : 18.319131 s.

with gcc -O :
loop set 1 : 10.119522 s.
loop set 2 : 9.289557 s.

with gcc -O3 -m486 -funroll-loops :
loop set 1 : 9.449567 s.
loop set 2 : 9.539554 s.

---
Bruno GILLETA
Laboratoire d'Informatique de Marseille

NH 3.2.1 (U)	D Gilleta-VF HP:120 Pw:55 AC:-11 N
		[+ )++ P- S+ ?D p+ $+ t s++ W-- E? PS? ?PP G- C-- I+
		@W? N b X-- So+ Sp+ !sb wb? 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019