iuradz
iuradz

Reputation: 1168

Will an SQLAlchemy class be slower than ordinary class?

I need to deal with some data that, in most cases, do not need to be written to disk. I'm using SQLAlchemy to deal with database operations. These data are from json strings. For example,

from sqlalchemy import String, Column
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class Class1(Base):
    __tablename__ = 'table'

    data = Column(String)
    #and some other data members
    @staticmethod
    def parse(json_):
        #parse the json and return a list of Class1 instances

class Class2():
    __init__(self, data):
        self.data = data
        #and some other data members
    @staticmethod
    def parse(json_):
        #parse the json and return a list of Class2 instances

Basically, these two class are the same except that Class1 can deal with database and Class2 cannot. Is there any performance difference between the two class when

  1. Create instances from json,
  2. Normal operations such as read data from a instance?

If there are performance difference, is there a good solution that can eliminate it while keeping DRY?

Upvotes: 0

Views: 40

Answers (2)

deets
deets

Reputation: 6395

There is certainly some difference, as the machinery of SQLAlchemy pulled in through the Base-class adds some overhead. But you are asking the wrong question. There might be a performance difference, but if it is crucial to your program, so that it justifies violating DRY principles, doesn't depend on this difference itself, but what you actual program overall does, and how it performs.

If it's fast enough to do its job - don't bother. If it is not fast enough, profile. See where your real bottlenecks are, don't anticipate.

Upvotes: 1

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83368

Based on your example code there isn't anything visible which would cause notable performance difference.

However you should benchmark the result for yourself if you believe there are micro-optimization which may help in your specific workflow.

See: Profiling Python How can you profile a python script?

Upvotes: 1

Related Questions