Salome HOME
Python 3 compatibility improvement (examples and tests)
[modules/adao.git] / test / test1002 / Performances.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2017 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22 "Test de fonctionnement et de performances de Numpy et Scipy"
23
24 # ==============================================================================
25 import numpy, time
26 numpy.set_printoptions(precision=5)
27
28 def testSysteme():
29     print("  Les caracteristiques des applications et outils systeme :")
30     import sys ; v=sys.version.split() ; print("    - Python systeme....: %s"%v[0])
31     import numpy ; print("    - Numpy.............: %s"%numpy.version.version)
32     try:
33         import scipy ; print("    - Scipy.............: %s"%scipy.version.version)
34     except:
35         print("    - Scipy.............: %s"%("absent",))
36     try:
37         import numpy.distutils.system_info as sysinfo ; la = sysinfo.get_info('lapack') ; print("    - Lapack............: %s/lib%s.so"%(la['library_dirs'][0],la['libraries'][0]))
38     except:
39         print("    - Lapack............: %s"%("absent",))
40     print("")
41     return True
42
43 def testNumpy01(dimension = 3, precision = 1.e-17, repetitions = 10):
44     "Test Numpy"
45     __d = int(dimension)
46     print("    Taille du test..................................: %.0e"%__d)
47     t_init = time.time()
48     A = numpy.array([numpy.arange(dimension)+1.,]*__d)
49     x = numpy.arange(__d)+1.
50     print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
51     #
52     t_init = time.time()
53     for i in range(repetitions):
54         b = numpy.dot(A,x)
55     print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
56     r = [__d*(__d+1.)*(2.*__d+1.)/6.,]*__d
57     if max(abs(b-r)) > precision:
58         raise ValueError("Resultat du test errone (1)")
59     else:
60         print("    Test correct, erreur maximale inferieure a %s"%precision)
61     print("")
62     del A, x, b
63
64 def testNumpy02(dimension = 3, precision = 1.e-17, repetitions = 100):
65     "Test Numpy"
66     __d = int(dimension)
67     print("    Taille du test..................................: %.0e"%__d)
68     t_init = time.time()
69     A = numpy.random.normal(0.,1.,size=(__d,__d))
70     x = numpy.random.normal(0.,1.,size=(__d,))
71     print("    La duree elapsed moyenne de l'initialisation est: %4.1f s"%(time.time()-t_init))
72     #
73     t_init = time.time()
74     for i in range(repetitions):
75         b = numpy.dot(A,x)
76     print("    La duree elapsed pour %3i produits est de.......: %4.1f s"%(repetitions, time.time()-t_init))
77     print("")
78     del A, x, b
79
80 # ==============================================================================
81 if __name__ == "__main__":
82     print('\nAUTODIAGNOSTIC\n')
83     testSysteme()
84     numpy.random.seed(1000)
85     testNumpy01(dimension = 1.e4)
86     testNumpy02(dimension = 3.e3)
87     print("")