Why is this rhombus so slow?

slow app!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

  int main(int argc, char** argv)
{
	
	int tamanio;

	//variables almacenamiento de datos (Rombo superior)
	int filasEspacioIzquierdo;
	int filasEspacioDerecho;

	//variables almacenamiento de datos (Rombo superior)
	int filasEspacioIzquierdo2;
	int filasEspacioDerecho2;
	
	cout << "Introducir el tamanio del triangulo: ";
	cin >> tamanio;
	cout << endl;
	
	//triangulo hemisferio superior 
	filasEspacioIzquierdo = tamanio;
	filasEspacioDerecho = 0;
	for(int serie = 1; serie <= tamanio; serie++)
	{
		//Espacio superior izquierda rombo
		for(int espacio = tamanio; espacio >= serie; espacio-- )
		{
			cout << " ";
		}
		
		//Area superior izquierda rombo
		for(int trianguloIzquierdo = tamanio	; trianguloIzquierdo >= filasEspacioIzquierdo; trianguloIzquierdo-- )
		{
			cout << "*";
		}
		filasEspacioIzquierdo--;
		
		//Area superior Derecha rombo
		for(int trianguloDerecho = 0; trianguloDerecho < filasEspacioDerecho; trianguloDerecho++)
		{
        	cout << "*";		
		}
		filasEspacioDerecho++;
		cout << endl;
	}
	
	
	//Triangulo Hemisferio inferior
	filasEspacioIzquierdo2 = 2;
	filasEspacioDerecho2 = 2;
	for(int serie2 = 2; serie2 <= tamanio; serie2++)
	{
		//Espacio inferior izquierda
		for( int espacio = 0;espacio < filasEspacioIzquierdo2; espacio++)
		{
			cout << " ";
		}	
		
		//Area inferior izquierda
		for(int trianguloIzquierdo = tamanio;trianguloIzquierdo >= filasEspacioIzquierdo2; trianguloIzquierdo-- )
		{
			cout << "*";
		}
		filasEspacioIzquierdo2++;
		
		//Espacio inferior derecha
		for(int trianguloDerecho = tamanio; trianguloDerecho > filasEspacioDerecho2; trianguloDerecho --)
		{
			cout << "*";
			
		}
		filasEspacioDerecho2++;
		
		
		cout << endl;
		
	}
	return 0;
}
Last edited on
Huh?

$ time ./a.out 
Introducir el tamanio del triangulo: 6

      *
     ***
    *****
   *******
  *********
 ***********
  *********
   *******
    *****
     ***
      *

real	0m4.137s
user	0m0.000s
sys	0m0.000s
$ time ./a.out 
Introducir el tamanio del triangulo: 6

      *
     ***
    *****
   *******
  *********
 ***********
  *********
   *******
    *****
     ***
      *

real	0m0.360s
user	0m0.000s
sys	0m0.000s

The real time is simply how quickly it took me to respond to the prompt.
The actual machine effort is insignificant.

What did you use to measure "slow" with?
And what were you expecting to consider "fast"?
> Why is this rhombus so slow?

It is not slow; it takes less than a millisecond to run the program.
http://coliru.stacked-crooked.com/a/76bc68ab756c5694
console output is 'relatively slow' and if you do enough of it redirection to a file is significantly faster (programname > file in both unix and dos will do this). Alternately you can buffer up all the output and drop it all in a single cout statement at the end (just += to a string variable).

Your program is too small to bother with this stuff -- you will barely see the change. If you wrote out thousands of lines, you would notice the difference.
In addition to what jonnin said, note that the use of std::endl will always flush the stream.
This usually does not matter for console I/O, because newlines are usually flushed anyway, but using std::endl when outputting to a file can make it much slower if endl is being called thousands of times.
https://en.cppreference.com/w/cpp/io/manip/endl
Last edited on
Topic archived. No new replies allowed.