Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:10:07+00:00 2026-06-08T11:10:07+00:00

Recently, I’ve discovered with the help of Jon Clements in this thread that the

  • 0

Recently, I’ve discovered with the help of Jon Clements in this thread that the following codes have very different execution times.

Do you have any idea why this is happening?

Comment: self.stream_data is a vector tuple with many zeros and int16 values and create_ZS_data method is performing so called ZeroSuppression.

Environment
Input: Many (3.5k) small files (~120kb each)
OS: Linux64
Python ver 2.6.8

Solution based on a generator:

def create_ZS_data(self):
    self.ZS_data = ( [column, row, self.stream_data[column + row * self.rows ]]
                     for row, column in itertools.product(xrange(self.rows), xrange(self.columns))
                     if self.stream_data[column + row * self.rows ] )

Profiler info:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     3257    1.117    0.000   71.598    0.022 decode_from_merlin.py:302(create_ZS_file)
   463419   67.705    0.000   67.705    0.000 decode_from_merlin.py:86(<genexpr>)

Jon’s Solution:

create_ZS_data(self):
    self.ZS_data = list()
    for rowno, cols in enumerate(self.stream_data[i:i+self.columns] for i in xrange(0, len(self.stream_data), self.columns)):
        for colno, col in enumerate(cols):
            # col == value, (rowno, colno) = index
            if col:
                self.ZS_data.append([colno, rowno, col])

Profiler info:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     3257   18.616    0.006   19.919    0.006 decode_from_merlin.py:83(create_ZS_data)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:10:10+00:00Added an answer on June 8, 2026 at 11:10 am

    I looked at the prior discussion; you seem to be troubled that your clever comprehension isn’t as efficient in cycles as it is in characters of source code. What I didn’t point out then was that this would be my preferred implementation to read:

    def sparse_table_elements(cells, columns, rows):
        ncells = len(cells)
        non_zeros = list()
        for nrow in range(0, ncells, columns):
             row = cells[nrow:nrow+columns]
             for ncol, cell in enumerate(row):
                 if cell:
                     non_zeros.append([ncol, nrow, cell])
        return non_zeros
    

    I’ve not tested it, but I can make sense of it. There are a couple of things that jump out at me as being potential inefficiencies. Recomputing the Cartesian product of two constant monotonically “boring” indices has got to be expensive:

    itertools.product(xrange(self.rows), xrange(self.columns))
    

    you then use the results [(0, 0), (0, 1), ...] to do single element indexing from your source:

    stream_data[column + row * self.rows]
    

    which is also more costly than handling larger slices as the “Jon’s” implementation does.

    Generators are not some secret sauce that guarantee efficiency. In this particular case, with 135kb of data that has already been read into core, a poorly constructed generator does seem to be costing you. If you want concise matrix operations, use APL; if you want readable code, don’t strive for rabid minimization in Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I found about this tool easy_install that help me to easy install additional
Recently, I was writing a class in which I discovered that I could reduce
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently, we discovered odd behavior in some old code. This code has worked for
Recently I was asked the following interview question: You have two sets of numbers
Recently I noticed (after others have extended the project) that the compile time significantly
Recently the PHP manual started showing the following warning on every mysql function page:
Recently I have released my app into the US AppStore. Now I Planed for
recently, while working on a db2 -> oracle migration project, we came across this
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.