Path: blob/master/src/sage/structure/list_clone_timings.py
8814 views
# -*- coding: utf-8 -*-1"""2Performance Test for Clone Protocol34see :class:`sage.structure.list_clone.ClonableArray`56EXAMPLES::78sage: from sage.structure.list_clone_timings import *9sage: cmd =["",10....: "e.__copy__()",11....: "copy(e)",12....: "e.clone()",13....: "e.__class__(e.parent(), e._get_list())",14....: "e.__class__(e.parent(), e[:])",15....: "e.check()",16....: "",17....: "add1_internal(e)",18....: "add1_immutable(e)",19....: "add1_mutable(e)",20....: "add1_with(e)",21....: "",22....: "cy_add1_internal(e)",23....: "cy_add1_immutable(e)",24....: "cy_add1_mutable(e)",25....: "cy_add1_with(e)"]2627Various timings using a Cython class::2829sage: size = 530sage: e = IncreasingArrays()(range(size))31sage: # random32....: for p in cmd: print "{0:36} : ".format(p),; timeit(p)33:34e.__copy__() : 625 loops, best of 3: 446 ns per loop35copy(e) : 625 loops, best of 3: 1.94 µs per loop36e.clone() : 625 loops, best of 3: 736 ns per loop37e.__class__(e.parent(), e._get_list()) : 625 loops, best of 3: 1.34 µs per loop38e.__class__(e.parent(), e[:]) : 625 loops, best of 3: 1.35 µs per loop39e.check() : 625 loops, best of 3: 342 ns per loop40:41add1_internal(e) : 625 loops, best of 3: 3.53 µs per loop42add1_immutable(e) : 625 loops, best of 3: 3.72 µs per loop43add1_mutable(e) : 625 loops, best of 3: 3.42 µs per loop44add1_with(e) : 625 loops, best of 3: 4.05 µs per loop45:46cy_add1_internal(e) : 625 loops, best of 3: 752 ns per loop47cy_add1_immutable(e) : 625 loops, best of 3: 1.28 µs per loop48cy_add1_mutable(e) : 625 loops, best of 3: 861 ns per loop49cy_add1_with(e) : 625 loops, best of 3: 1.51 µs per loop5051Various timings using a Python class::5253sage: e = IncreasingArraysPy()(range(size))54sage: # random55....: for p in cmd: print "{0:36} : ".format(p),; timeit(p)56:57e.__copy__() : 625 loops, best of 3: 869 ns per loop58copy(e) : 625 loops, best of 3: 2.13 µs per loop59e.clone() : 625 loops, best of 3: 1.86 µs per loop60e.__class__(e.parent(), e._get_list()) : 625 loops, best of 3: 7.52 µs per loop61e.__class__(e.parent(), e[:]) : 625 loops, best of 3: 7.27 µs per loop62e.check() : 625 loops, best of 3: 4.02 µs per loop63:64add1_internal(e) : 625 loops, best of 3: 9.34 µs per loop65add1_immutable(e) : 625 loops, best of 3: 9.91 µs per loop66add1_mutable(e) : 625 loops, best of 3: 12.6 µs per loop67add1_with(e) : 625 loops, best of 3: 15.9 µs per loop68:69cy_add1_internal(e) : 625 loops, best of 3: 7.13 µs per loop70cy_add1_immutable(e) : 625 loops, best of 3: 6.95 µs per loop71cy_add1_mutable(e) : 625 loops, best of 3: 14.1 µs per loop72cy_add1_with(e) : 625 loops, best of 3: 17.5 µs per loop73"""74#*****************************************************************************75# Copyright (C) 2009-2010 Florent Hivert <[email protected]>76#77# Distributed under the terms of the GNU General Public License (GPL)78# http://www.gnu.org/licenses/79#*****************************************************************************808182from sage.structure.list_clone import ClonableArray83from sage.structure.list_clone_demo import IncreasingArrays84from sage.structure.list_clone_timings_cy import *8586class IncreasingArraysPy(IncreasingArrays):8788class Element(ClonableArray):89"""90A small class for testing :class:`ClonableArray`: Increasing Lists9192TESTS::9394sage: from sage.structure.list_clone_timings import IncreasingArraysPy95sage: TestSuite(IncreasingArraysPy()([1,2,3])).run()96"""9798def check(self):99"""100Check that ``self`` is increasing.101102EXAMPLES::103104sage: from sage.structure.list_clone_timings import IncreasingArraysPy105sage: IncreasingArraysPy()([1,2,3]) # indirect doctest106[1, 2, 3]107sage: IncreasingArraysPy()([3,2,1]) # indirect doctest108Traceback (most recent call last):109...110ValueError: Lists is not increasing111"""112for i in range(len(self)-1):113if self[i] >= self[i+1]:114raise ValueError, "Lists is not increasing"115116117#####################################################################118###### Timings functions ######119#####################################################################120def add1_internal(bla):121"""122TESTS::123124sage: from sage.structure.list_clone_timings import *125sage: add1_internal(IncreasingArrays()([1,4,5]))126[2, 5, 6]127"""128blo = bla.__copy__()129lst = blo._get_list()130for i in range(len(blo)): lst[i] += 1131blo.set_immutable()132blo.check()133return blo134135def add1_immutable(bla):136"""137TESTS::138139sage: from sage.structure.list_clone_timings import *140sage: add1_immutable(IncreasingArrays()([1,4,5]))141[2, 5, 6]142"""143lbla = bla[:]144for i in range(len(lbla)): lbla[i] += 1145return bla.__class__(bla.parent(), lbla)146147def add1_mutable(bla):148"""149TESTS::150151sage: from sage.structure.list_clone_timings import *152sage: add1_mutable(IncreasingArrays()([1,4,5]))153[2, 5, 6]154"""155blo = bla.__copy__()156for i in range(len(blo)): blo[i] += 1157blo.set_immutable()158blo.check()159return blo160161def add1_with(bla):162"""163TESTS::164165sage: from sage.structure.list_clone_timings import *166sage: add1_with(IncreasingArrays()([1,4,5]))167[2, 5, 6]168"""169with bla.clone() as blo:170for i in range(len(blo)): blo[i] += 1171return blo172173174