Path: blob/master/sage/structure/list_clone_timings.py
4056 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 ClonableArray, IncreasingArrays83from sage.structure.list_clone_timings_cy import *8485class IncreasingArraysPy(IncreasingArrays):8687class Element(ClonableArray):88"""89A small class for testing :class:`ClonableArray`: Increasing Lists9091TESTS::9293sage: from sage.structure.list_clone_timings import IncreasingArraysPy94sage: TestSuite(IncreasingArraysPy()([1,2,3])).run()95"""9697def check(self):98"""99Check that ``self`` is increasing.100101EXAMPLES::102103sage: from sage.structure.list_clone_timings import IncreasingArraysPy104sage: IncreasingArraysPy()([1,2,3]) # indirect doctest105[1, 2, 3]106sage: IncreasingArraysPy()([3,2,1]) # indirect doctest107Traceback (most recent call last):108...109AssertionError: Lists is not increasing110"""111for i in range(len(self)-1):112assert self[i] < self[i+1], "Lists is not increasing"113114115#####################################################################116###### Timings functions ######117#####################################################################118from list_clone import *119from list_clone_timings_cy import *120121def add1_internal(bla):122"""123TESTS::124125sage: from sage.structure.list_clone_timings import *126sage: add1_internal(IncreasingArrays()([1,4,5]))127[2, 5, 6]128"""129blo = bla.__copy__()130lst = blo._get_list()131for i in range(len(blo)): lst[i] += 1132blo.set_immutable()133blo.check()134return blo135136def add1_immutable(bla):137"""138TESTS::139140sage: from sage.structure.list_clone_timings import *141sage: add1_immutable(IncreasingArrays()([1,4,5]))142[2, 5, 6]143"""144lbla = bla[:]145for i in range(len(lbla)): lbla[i] += 1146return bla.__class__(bla.parent(), lbla)147148def add1_mutable(bla):149"""150TESTS::151152sage: from sage.structure.list_clone_timings import *153sage: add1_mutable(IncreasingArrays()([1,4,5]))154[2, 5, 6]155"""156blo = bla.__copy__()157for i in range(len(blo)): blo[i] += 1158blo.set_immutable()159blo.check()160return blo161162def add1_with(bla):163"""164TESTS::165166sage: from sage.structure.list_clone_timings import *167sage: add1_with(IncreasingArrays()([1,4,5]))168[2, 5, 6]169"""170with bla.clone() as blo:171for i in range(len(blo)): blo[i] += 1172return blo173174175