Python Forum
Are there optimizer for static code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are there optimizer for static code?
#1
Python 3.7.1

My program works with big data, where I don't see the point of using dynamic language capabilities (adding class members or methods during program execution). Also I try to create a good application architecture, that has multiple layers. All this makes the callstack very long, and the program runs slowly. In fact, my program is written in C++ style.

Is it possible to optimize my code, by indicating that I will abandon the dynamic capabilities of the Python? I know there is PyPy (which uses embedding of small functions, which reduces stack depth), but it requires refusal libraries written in C, which is unacceptable in the case of machine learning. What other optimization tools are available that allow you to create fast programs with good architecture?
Reply
#2
it wouldn't be static if optimized, right?
Reply
#3
(Nov-01-2019, 05:02 PM)Larz60+ Wrote: it wouldn't be static if optimized, right?

On the contrary: my code is static and therefore can be optimized.
Reply
#4
I found a way using Cython:
inline- operator
Early binding

Are there other ways?
Reply
#5
For example classes have dicts for their methods and attributes.
If I remind right, a dict has a size of 112 bytes (since Python 3.6).
If you have only 10-1000 objects, you just don't care.
But with one million objects, you care about memory consumption.
In this case, use __slots__ for this kind of classes.

If you have a for-loop, try to solve this not in Python.
Looping is not very efficient in Python. You can do this in cython.

Maybe you can post some example code with example data.


EDIT: Very often constructs like a list comprehension are faster than using generators. Generators are used, if you have much data to process and want to solve this in a iterative way and saving memory consumption. Maybe you can also implement a generator in Cython, which should run faster and saves memory.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(Nov-02-2019, 08:57 AM)DeaD_EyE Wrote: Maybe you can post some example code with example data.

Presenting the code will be difficult, because it is scattered across multiple files. But the main idea is quite simple to state: I have a set of csv- files, separated by the month. My task is to execute high-level business logic on each line from these files. The architect is represented by the following components:
1. Command - works with business logic for each line of file.
2. Storage - allows to change the way data is stored (files, database, internet, generation...), i.e. makes a call to the selected method of data storage.
3. DatŠ° - organizes enumeration of files in the desired date range.
4. Converter - convert data from str to needed types.
5. File - actually receives the data from csv- file.

See how deep a call stack is for each line of data from a file? With such a stack, the overhead significantly exceeds the complexity of the useful work (i.e. parsing the string).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem usage of static method akbarza 5 553 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Static type checking with PyPy pitosalas 1 460 Dec-14-2023, 09:29 PM
Last Post: deanhystad
  Class member become static Quasar999 1 683 Sep-16-2023, 12:52 PM
Last Post: deanhystad
Photo Output Static image on HDMI2 random816382 0 1,412 Oct-18-2021, 11:21 AM
Last Post: random816382
Question Google Foobar- Code works in my IDE but not in foobar. Static method? pr3ttykitty 4 4,961 Feb-24-2021, 05:03 PM
Last Post: nilamo
  Using static methods for library? giu88 4 2,943 Sep-12-2018, 07:52 AM
Last Post: giu88
  Notebook is rendered as static html by github miner_tom 2 3,125 Aug-23-2018, 09:00 PM
Last Post: perfringo
  need to alter static picture to video file mika 1 2,759 Feb-23-2018, 02:11 PM
Last Post: ka06059
  Updating & Accessing a "Static" List Bass 4 4,439 May-23-2017, 07:12 PM
Last Post: Bass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020