1
1
import time
2
2
import numpy as np
3
3
import plotly .graph_objs as go
4
+ import pytest
4
5
5
6
np .random .seed (1 )
6
7
@@ -14,14 +15,18 @@ def test_performance_b64_scatter3d():
14
15
c = np .random .randint (size = N , low = - 10 , high = 10 , dtype = "int8" )
15
16
16
17
# Test the performance with lists
18
+ x_list = x .tolist ()
19
+ y_list = y .tolist ()
20
+ z_list = z .tolist ()
21
+ c_list = c .tolist ()
17
22
list_start = time .time ()
18
23
fig = go .Figure (
19
24
data = [
20
25
go .Scatter3d (
21
- x = x . tolist () ,
22
- y = y . tolist () ,
23
- z = z . tolist () ,
24
- marker = dict (color = c . tolist () ),
26
+ x = x_list ,
27
+ y = y_list ,
28
+ z = z_list ,
29
+ marker = dict (color = c_list ),
25
30
mode = "markers" ,
26
31
opacity = 0.2 ,
27
32
)
@@ -41,31 +46,46 @@ def test_performance_b64_scatter3d():
41
46
)
42
47
np_time_elapsed = time .time () - np_start
43
48
44
- # np should be faster than raw
45
- assert (np_time_elapsed / list_time_elapsed ) < 0.75
49
+ # np should be faster than lists
50
+ assert (np_time_elapsed / list_time_elapsed ) < 0.005
46
51
47
52
48
53
def test_performance_b64_float64 ():
49
54
np_arr_1 = np .random .random (10000 )
50
55
np_arr_2 = np .random .random (10000 )
56
+ list_1 = np_arr_1 .tolist ()
57
+ list_2 = np_arr_2 .tolist ()
51
58
52
59
# Test the performance of the base64 arrays
53
60
np_start = time .time ()
54
61
fig = go .Scatter (x = np_arr_1 , y = np_arr_2 )
55
62
np_time_elapsed = time .time () - np_start
56
63
57
- # Test the performance of the raw arrays
64
+ # Test the performance of the normal lists
58
65
list_start = time .time ()
59
- fig = go .Scatter (x = np_arr_1 . tolist () , y = np_arr_2 . tolist () )
66
+ fig = go .Scatter (x = list_1 , y = list_2 )
60
67
list_time_elapsed = time .time () - list_start
61
68
62
- # np should be faster than raw
63
- assert (np_time_elapsed / list_time_elapsed ) < 0.75
69
+ # np should be faster than lists
70
+ assert (np_time_elapsed / list_time_elapsed ) < 0.3
64
71
65
72
66
- def test_size_performance_b64_uint8 ():
67
- np_arr_1 = (np .random .random (100000 ) * 256 ).astype ("uint8" )
68
- np_arr_2 = (np .random .random (100000 ) * 256 ).astype ("uint8" )
73
+ DTYPE_TEST_CASES = [
74
+ (
75
+ "uint8" , # dtype
76
+ 256 , # max_val
77
+ 400000 # difference threshold
78
+ ),
79
+ (
80
+ 'uint32' ,
81
+ 2 ** 32 ,
82
+ 900000
83
+ )
84
+ ]
85
+ @pytest .mark .parametrize ('dtype, max_val, expected_size_difference' , DTYPE_TEST_CASES )
86
+ def test_size_performance_b64_uint8 (dtype , max_val , expected_size_difference ):
87
+ np_arr_1 = (np .random .random (100000 ) * max_val ).astype (dtype )
88
+ np_arr_2 = (np .random .random (100000 ) * max_val ).astype (dtype )
69
89
70
90
# Measure the size of figures with numpy arrays
71
91
fig_np = go .Scatter (x = np_arr_1 , y = np_arr_2 )
@@ -75,5 +95,5 @@ def test_size_performance_b64_uint8():
75
95
fig_list = go .Scatter (x = np_arr_1 .tolist (), y = np_arr_2 .tolist ())
76
96
size_list = fig_list .to_json ().__sizeof__ ()
77
97
78
- # np should be smaller than raw
79
- assert size_list - size_np > 1000
98
+ # np should be smaller than lists
99
+ assert size_list - size_np > expected_size_difference
0 commit comments