# Development/Efficiency: test_dict.py

File test_dict.py, 4.4 KB (added by jdsiiro, 5 years ago) |
---|

Line | |
---|---|

1 | #!/usr/bin/env python |

2 | from math import sqrt |

3 | from sys import stdout as out, version_info |

4 | from timeit import timeit |

5 | |

6 | out.write('='*72+"\nTesting on Python %s\n" % ( |

7 | '.'.join(str(x) for x in version_info[:3]),) ) |

8 | |

9 | N=1 |

10 | |

11 | unknown = {} |

12 | intDict = {} |

13 | strDict = {} |

14 | for i, v in enumerate(str(i)*N for i in range(10000)): |

15 | intDict[i] = v |

16 | strDict[v] = i |

17 | |

18 | setup = "from __main__ import %s as d, unknown; v=%r" |

19 | |

20 | tests = [ |

21 | "d[v]", |

22 | "d.get(v)", |

23 | "d.get(v, 5)", |

24 | "d.get(v, {})", |

25 | "d.get(v, unknown)", |

26 | "None if v not in d else d[v]", |

27 | "d[v] if v in d else None", |

28 | "d[v] if v in d else 5", |

29 | "d[v] if v in d else {}", |

30 | "d[v] if v in d else unknown", |

31 | "try: d[v]\nexcept: pass", |

32 | "if v in d: d[v]", |

33 | ] |

34 | testFmt = "%%-%dr" % (max(len("%r"%t) for t in tests) + 2, ) |

35 | for src in ('intDict', 'strDict'): |

36 | for key in (1234,"1234"*N,-1234 if src == 'intDict' else "-"+"1234"*N): |

37 | out.write( |

38 | "\nTesting getting data from dict; d=%s, v=%r, v in d is %s\n" |

39 | % (src, key, eval('%r in %s' % (key, src))) ) |

40 | for test in tests: |

41 | try: |

42 | trials = [ timeit(test, setup % (src, key), number=10000000) |

43 | for i in range(3) ] |

44 | mean = sum(trials) / float(len(trials)) |

45 | stdev = sqrt( sum(x**2 for x in trials)/float(len(trials)) |

46 | - mean**2 ) |

47 | out.write( |

48 | testFmt % test + " %0.6f +/- %0.3f\n" % (mean, stdev) ) |

49 | except KeyboardInterrupt: |

50 | raise SystemExit |

51 | except: |

52 | out.write( testFmt % test + "(exception)\n" ) |

53 | out.flush() |

54 | |

55 | |

56 | ####################################################################### |

57 | |

58 | setup = "from __main__ import %s as d, unknown; v=%r" |

59 | core = "%s\nif not %s: del d[v]" |

60 | tests = [ |

61 | "d[v] = 1", |

62 | "d.setdefault(v, 1)", |

63 | "if v not in d: d[v] = 1", |

64 | "a = d.setdefault(v, 1)", |

65 | "if v in d:\n a = d[v]\nelse:\n a = 1\n d[v] = a", |

66 | "if v not in d:\n a = 1\n d[v] = a\nelse:\n a = d[v]", |

67 | ] |

68 | |

69 | testFmt = "%%-%dr" % (max(len("%r"%t) for t in tests) + 2, ) |

70 | for src in ('intDict', 'strDict'): |

71 | for key in (1234,"1234"*N,-1234 if src == 'intDict' else "-"+"1234"*N): |

72 | out.write( |

73 | "\nTesting set default data in dict; d=%s, v=%r, v in d is %s\n" |

74 | % (src, key, eval('%r in %s' % (key, src))) ) |

75 | for test in tests: |

76 | try: |

77 | trials = [ timeit( core % (test, eval('%r in %s' % (key,src))), |

78 | setup % (src, key), |

79 | number=10000000) |

80 | for i in range(3) ] |

81 | mean = sum(trials) / float(len(trials)) |

82 | stdev = sqrt( sum(x**2 for x in trials)/float(len(trials)) |

83 | - mean**2 ) |

84 | out.write( |

85 | testFmt % test + " %0.6f +/- %0.3f\n" % (mean, stdev) ) |

86 | except KeyboardInterrupt: |

87 | raise SystemExit |

88 | except: |

89 | out.write( testFmt % test + "(exception)\n" ) |

90 | raise |

91 | out.flush() |

92 | |

93 | ####################################################################### |

94 | |

95 | setup = "from __main__ import %s as d, unknown" |

96 | |

97 | tests=[ |

98 | "ans=dict(d)", |

99 | "ans=dict(d.iteritems())", |

100 | "ans=dict(d.items())", |

101 | "ans=dict((i,v) for i,v in d.iteritems())", |

102 | "ans=dict((i,v) for i,v in d.items())", |

103 | "ans={}\nfor i in d: ans[i] = d[i]", |

104 | "ans={}\nfor i,v in d.iteritems(): ans[i] = v", |

105 | "ans={}\nfor i,v in d.items(): ans[i] = v", |

106 | "ans={}\nans.update((i,v) for i,v in d.iteritems())", |

107 | "ans={}\nans.update((i,v) for i,v in d.items())", |

108 | ] |

109 | |

110 | testFmt = "%%-%dr" % (max(len("%r"%t) for t in tests) + 2, ) |

111 | for src in ('intDict', 'strDict'): |

112 | out.write("\nTesting constructing dict; d=%s\n" % (src, )) |

113 | for test in tests: |

114 | try: |

115 | trials = [ timeit(test, setup % (src, ), number=1000) |

116 | for i in range(3) ] |

117 | mean = sum(trials) / float(len(trials)) |

118 | stdev = sqrt( sum(x**2 for x in trials)/float(len(trials)) |

119 | - mean**2 ) |

120 | out.write( |

121 | testFmt % test + " %0.6f +/- %0.3f\n" % (mean, stdev) ) |

122 | except KeyboardInterrupt: |

123 | raise SystemExit |

124 | except: |

125 | out.write( testFmt % test + "(exception)\n" ) |

126 | out.flush() |