Efficient code implementation

I have this code to print the array below. The array comes from another array size 8x8 initialise with random numbers.
1
2
3
4
5
6
7
12 5  6 34 
10 45 5 23 
20 89 76 5 
11 34 3 44 
34 2  3 
22 89 
72 


Here is my code. It works fine but I find it very lengthy. Any tips on how to make this shorter?
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
int array[7][4];

for(int i = 0; i < 4; i++)
{
 array[0][i] = matrix[0][i+3]; 
 cout << array[0][i] << " ";
}
cout << endl;

for(int i = 0; i < 4; i++)
{
 array[1][i] = matrix[1][i+2]; 
 cout << array[1][i] << " ";
}

cout << endl;

for(int i = 0; i < 4; i++)
{
 array[2][i] = matrix[2][i+1]; 
 cout << array[2][i] << " ";
}

cout << endl;

for(int i = 0; i < 4; i++)
{
 array[3][i] = matrix[3][i];
 cout << array[3][i] << " ";
}

cout << endl;

for(int i = 0; i < 3; i++)
{
 array[4][i] = matrix[4][i];
 cout << array[4][i] << " ";
}

cout << endl;

for(int i = 0; i < 2; i++)
{
 array[5][i] = matrix[5][i]; 
 cout << array[5][i] << " ";
}

cout << endl;

array[6][0] = matrix[6][0]; 
cout << array[6][0] << " ";
cout << endl;
Hi stbb24,

You have a lot of these:

for(int i = 0; i < 4; i++)

can you combine all the code in the similar loops into one for loop?


Edit: A better clue: a for loop inside another for loop?
Last edited on
closed account (zb0S216C)
I can think of a few ways to improve you code in terms of efficiency:

1) i is created and destroy multiple times. Remove i from all of the loops, and declare i before the first loop. For instance:

1
2
3
4
5
6
7
8
9
10
int i_(0);
for(; i_ < 4; ++i_)
{
    // ...
}

for(i_ = 0; i_ < 3; ++i_)
{
    // ...
}

This prevents Stack Thrashing.

2) I recommend switching i++ with ++i. The pre-fix increment/decrement is more efficient because post-fix increment/decrement pushes an extra int onto the stack and returns a copy of the original operand.

3) Use std::endl() less. Excessive calls to std::endl() will hinder performance. Replace each occurrence with '\n' and before the end of the program, flush the stream with std::cout.flush().

Wazzak
Last edited on
@Framework
Are you serious? The program is clearly I/O bound, saving a few processor ticks won't change anything. If we are talking about performance, I think using C stdout could help.

As for the original question - obviously you should use nested loops.
closed account (zb0S216C)
@Abramus:

Judging by the title, he/she wanted efficiency, so I gave tips on how the code could be improved, so stop moaning. By the way, how is the file system faster than memory? How could that improve efficiency?

Wazzak
He asked how to make it shorter actually, and I guess the obvious way is to nest for loops:
1
2
3
4
5
6
7
8
const int sub[7] = {0,0,0,0,1,2,3};
for(size_t i = 0; i < 7; ++i)
{
for(size_t w = 0; w < 4-sub[i]; ++w)
{
array[i][w] = array[i][w+sub[6-i];
}
}

Although you need to realize that you're trying to access past the end of your array.
He want to implement efficiently. To write code in an efficient way.
(he explicit that he wants it shorter)

Don't sweat the small stuff. ¿Why losing time in things that the compiler should be doing automatically?
how is the file system faster than memory? How could that improve efficiency?
It is not. That's way you should attack it first.
may be this will help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for( int i = 0 ; i <6 ; i++ ) 
	{
		for(int j = 0 ; j < 4 ; j++ ) 
		{
			array[i][j] = matrix[i][j]
			if(  ( i == 4 ) && ( j == 3) 
				break;

			if(  ( i ==5 ) && ( j == 2) 
				break;

			if(  ( i ==6 ) && ( j == ) 
				break;
		}

	}
Thanks for everyones replies :)
I was not moaning. I was simply saying that the "tricks" you presented could be potentialy of any use for a person who wants to optimize a CPU bottleneck in his program. Trying to use them in any other situation is counterproductive and a waste of time. Besides, as others mentioned, the original question is rather about writing succinct code.

By saying stdout, I meant using printf rather than cout. This could actually improve efficiency, unlike your suggestions (except for 3rd).
Topic archived. No new replies allowed.