summary refs log blame commit diff stats
path: root/test/stuff/test4.py
blob: b2390a036b1229ea4a19e55c1f9a0061dfe790f2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highli
import random, time

class DelValue():
	def a(d):
		return dict((k, v) for k, v in d.items() if v is not 0)

	def b(d):
		for k, v in d.copy().items():
			if v == 0: del d[k]
		return d

	def c(d):
		for k in tuple(d.keys()):
			if d[k] == 0: del d[k]
		return d

	def d(d):
		for k, v in tuple(d.items()):
			if v == 0: del d[k]
		return d


basedict = {}
for i in range(200):
	basedict[i] = random.randint(0, 1)

expected = DelValue.a(basedict.copy())

for algo in ['a', 'b', 'c', 'd']:
	copy = basedict.copy()
	t = time.time()
	for i in range(100):
		assert DelValue.__dict__[algo](copy) == expected
	print("algorithm %s: %f" % (algo, time.time() - t))

# c it is, although b is faster with smaller dictionaries