Giter Site home page Giter Site logo

plain_obj's Introduction

plain_obj

https://travis-ci.org/suzaku/plain_obj.svg?branch=master

A faster alternative to namedtuple.

Basic Usage

Creation

import plain_obj
Config = plain_obj.new_type('Config', 'is_debug, skips_dist, run_tests')
config = Config(True, False, True)
if config.is_debug:
    print("This is a verbose debugging message.")

Make a dict

config.as_dict()

Unpacking

is_debug, _, run_tests = config

When to use plain_obj instead of namedtuple?

When faster creation time matters to you.

Comparing plain_obj with namedtuple in Python 2.7:

In [3]: %timeit collections.namedtuple('Point', ['x', 'y', 'z'])
1000 loops, best of 3: 338 µs per loop

In [4]: %timeit plain_obj.new_type('Point', ['x', 'y', 'z'])
10000 loops, best of 3: 97.8 µs per loop

In [5]: Point = collections.namedtuple('Point', ['x', 'y', 'z'])

In [6]: NewPoint = plain_obj.new_type('Point', ['x', 'y', 'z'])

In [7]: %timeit Point(1, 2, 3)
The slowest run took 7.99 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 507 ns per loop

In [8]: %timeit NewPoint(1, 2, 3)
The slowest run took 6.70 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 462 ns per loop

In [9]: p = Point(1, 2, 3)

In [10]: new_p = NewPoint(1, 2, 3)

In [11]: %timeit p.x, p.y, p.z
The slowest run took 9.92 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 408 ns per loop

In [12]: %timeit new_p.x, new_p.y, new_p.z
The slowest run took 11.70 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 163 ns per loop

Comparing plain_obj with namedtuple in Python 3.6:

In [3]: %timeit collections.namedtuple('Point', ['x', 'y', 'z'])
382 µs ± 3.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [4]: %timeit plain_obj.new_type('Point', ['x', 'y', 'z'])
53.5 µs ± 1.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [5]: Point = collections.namedtuple('Point', ['x', 'y', 'z'])

In [6]: NewPoint = plain_obj.new_type('Point', ['x', 'y', 'z'])

In [7]: %timeit Point(1, 2, 3)
521 ns ± 2.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [8]: %timeit NewPoint(1, 2, 3)
438 ns ± 5.53 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [9]: p = Point(1, 2, 3)

In [10]: new_p = NewPoint(1, 2, 3)

In [11]: %timeit p.x, p.y, p.z
282 ns ± 2.52 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [12]: %timeit new_p.x, new_p.y, new_p.z
148 ns ± 1.7 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

As you can see, it's faster in all cases including type creation, object instantiation and attribute access.

Sponsor

plain_obj's People

Contributors

suzaku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

plain_obj's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.