Bonjour,

Je lis un fichier binaire qui contient 3601*3601 altitudes codées sur 16 bits au format dted 2 data (environ 25 MO), et j'en fait la moyenne.

En python il faut environ 16 secondes. J'ai codé la même chose en c++ et il me faut 0.5 secondes soit 32 fois plus performant.

Est ce normal, ou est ce mon code python qui est mal écrit ?

python :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
path = "N47E006.hgt"
longitude_line_count = 3601
latitude_point_count = 3601
 
dt1_file = open( path, 'rb' )
 
mean_z = 0
 
for longitude_id in range( longitude_line_count ) :
	for latitude_id in range( latitude_point_count ) :
		value = dt1_file.read(2)
		high_byte = ord( value[0] )
		low_byte = ord( value[1] )
		sign = 1 - ( ( high_byte & 0x80 ) / 0x80 ) * 2
		high_byte = high_byte & 0x7f
		z = sign * ( high_byte * 0x100 + low_byte )
 
		mean_z += z
 
print( "mean_z      : " + str( float(mean_z) / float( longitude_line_count * latitude_point_count ) ) )
 
dt1_file.close()
c++
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <fstream>
#include <iostream>
 
long read( std::ifstream & stream )
{
	char data[2];
	stream.read( data, 2 );
 
	long const sign = 1 - ( ( data[0] & 0x80 ) / 0x80 ) * 2;
	data[0] = data[0] & 0x7f;
 
	return sign * (long)( (data[0] * 0x100) | data[1] );
}
 
int main()
{
	std::ifstream file("N47E006.hgt", std::ios_base::in | std::ios_base::binary );
 
	size_t const longitude_count = 3601;
	size_t const latitude_count = 3601;
 
	long z_sum = 0;
 
	char data[2]; // hight byte, low byte
 
	for( size_t longitude = 0; longitude<longitude_count; ++longitude )
	{
		for( size_t latitude = 0; latitude<latitude_count; ++latitude )
		{
			z_sum += read( file );
		}
	}
 
	std::cout << "mean z      : " << static_cast<float>(z_sum) / static_cast<float>(longitude_count * latitude_count) << std::endl;
 
	file.close();
}