惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
T
Tenable Blog
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
P
Privacy & Cybersecurity Law Blog
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
H
Help Net Security
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
S
Schneier on Security
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
P
Privacy International News Feed
S
Secure Thoughts
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
博客园 - Franky
T
Tor Project blog
G
GRAHAM CLULEY
博客园 - 【当耐特】
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
Hacker News - Newest:
Hacker News - Newest: "LLM"

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode So Far So Rust Retrying HTTP Requests with Rust A Primer on Control Charts Learning Rust Explicit is Better than Implicit: Rust for Pythonistas Using Custom Delimiters in Jinja Templates TIL: Creating Fixed Length Iterables in Python Documentation Without Assumption Vagrant Python - A Reflection in 2022 Learning Golang No, A Virtual Machine Is Not Enough: Why Developers Need Native Linux Empathy in Tech For Those Who Came in Late A Weekend With PostgreSQL TIL: Gooey and Python Fire for Quick GUIs and CLIs TIL: 2ality - Dr. Axel Rauschmayer's JavaScript Blog TIL: MassDNS - High-Performance Bulk DNS Lookups TIL: Matomo Analytics, Google Tech Writing, Memory Programming, and NES TV Signals TIL: MontyDB - MongoDB Implemented in Python Returning to the Craft of Programming TIL: CPUFetch, OneFetch, and Learn CSS TIL: DNS Performance Testing and Pi-hole with Unbound TIL: Eli Bendersky's Blog, Awesome By Example, NoCoDB, and Martin Kleppmann TIL: CRDTs, Extreme HTTP Performance, and BYTEPATH Game TIL: AutoInvent, ASGI, Python Packaging, RAPIDS GPU Computing, and FlaskCon TIL: MangaDesk - Terminal Client for MangaDex TIL: McFly - Smart Shell History Search TIL: Siege Load Testing and Awesome FastAPI Resources TIL: Ventoy Bootable USB and Justniffer Network Analysis TIL: CLI Code Review, Git Split Diffs, and Internal Combustion Engine TIL: Benford's Law, Web Security Headers, Event Sourcing, and Mozilla Security Guidelines How to Write Documentation - The README.md File The Importance of Documentation TIL: NNgroup UX Research, SponsorBlock, and Labella Python Library TIL: The Little Book of Rust Macros and Rust Performance Book TIL: Git-Bug Distributed Issue Tracker and Omni Kubernetes Monitoring TIL: Zellij - Modern Terminal Multiplexer TIL: How Discord Handles 2.5 Million Concurrent Voice Users TIL: Volumio - The Audiophile Music Player TIL: Areopagitica - Milton's Defense of Free Speech TIL: Fast Node Manager, Zoxide Smart CD, Technical Writing, PyO3, and Qubes OS TIL: Slurm Workload Manager for HPC Clusters TIL: Data Visualization Guide and Oso Authorization Academy TIL: CORS Deep Dive, Piku Tiny PaaS, Rust Strings, and Deno Standard Library TIL: Raspberry Pi OS Development, Vim Beginner Guide, Password Management, and QueryBook TIL: uBlock Origin Performance Optimization on Firefox TIL: Breaking PostgreSQL at Scale and LeetCode Problem Patterns TIL: Awesome Tmux Resources for Terminal Multiplexing TIL: Grit - A Multitree-Based Personal Task Manager TIL: Lens 4.2 Kubernetes IDE, Shell Scripting Guide, and Dark HTTP Server Do The Job You Hate So You Won't Hate The Job You Love TIL: Innernet VPN Solution and NoteCalc Calculator App TIL: Argo CD for GitOps and Lens Kubernetes IDE TIL: Modern Rust CLI Tools - System Monitoring, HTTP Requests, and DNS TIL: tz - A Time Zone Helper Tool TIL: Distributed Systems Education, Fallacies, and Self-Hosted Internet Archiving TIL: Real-Time Voice Cloning Technology TIL: ChartMuseum for Helm, AMD's Corporate Journey, and Kubernetes Pod Scaling TIL: Docker and Kubernetes Tools - Whaler, Descheduler, and Dive TIL: Post-Mortem Collection, Terminal Plotting, and Technical Twitter TIL: Dark Mode Toggle Web Component by Google Chrome Labs TIL: Python eval(), exec(), and compile() Functions TIL: Camelot PDF Tables, PostgreSQL Row Level Security, Zerodha Varsity, and Write Yourself a Git TIL: fuser Command for Process and File Investigation TIL: i Hate Regex - The Ultimate Regex Cheat Sheet TIL: Dolt - Git for Data and Database Version Control TIL: x86 Assembly Programming and SafeEyes Break Reminder TIL: Comprehensive Distributed Systems Reading List TIL: Cosmopolitan C Library, Distributed Systems Book, High Performance Browser Networking, and Rust Roguelike Tutorial
TIL: Python Performance Optimization and Advanced Profiling
2020-09-09 · via Stonecharioteer on Tech

Today I explored comprehensive Python performance optimization techniques and discovered advanced profiling tools that provide deep insights into CPU usage, memory allocation, and execution bottlenecks.

Scalene represents a new generation of Python profilers that provides detailed CPU and memory analysis:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pandas as pd
import numpy as np
from typing import List, Tuple
import warnings
warnings.filterwarnings('ignore')

# Critical pandas performance tip from archive:
# Using & and | operators is MUCH faster than zip() with any() or all()

def slow_pandas_filtering(df: pd.DataFrame) -> pd.DataFrame:
    """Inefficient: using zip with multiple conditions"""
    condition1 = df['column1'] > 100
    condition2 = df['column2'] < 50
    condition3 = df['column3'] == 'target'

    # SLOW: Don't do this!
    combined_condition = [
        any([c1, c2, c3])
        for c1, c2, c3 in zip(condition1, condition2, condition3)
    ]

    return df[combined_condition]

def fast_pandas_filtering(df: pd.DataFrame) -> pd.DataFrame:
    """Efficient: using pandas boolean operators"""
    # FAST: Use & and | operators directly
    condition = (
        (df['column1'] > 100) |
        (df['column2'] < 50) |
        (df['column3'] == 'target')
    )

    return df[condition]

def even_faster_pandas_filtering(df: pd.DataFrame) -> pd.DataFrame:
    """Even faster: use .loc with intermediate DataFrames if needed"""
    # For complex filtering, sometimes multiple .loc calls are faster
    filtered = df.loc[df['column1'] > 100]  # First filter
    filtered = filtered.loc[filtered['column2'] < 50]  # Second filter
    filtered = filtered.loc[filtered['column3'] == 'target']  # Third filter

    return filtered

# Advanced pandas optimization techniques
class PandasOptimizer:
    """Collection of pandas optimization techniques"""

    @staticmethod
    def optimize_dtypes(df: pd.DataFrame) -> pd.DataFrame:
        """Optimize DataFrame memory usage by choosing appropriate dtypes"""
        optimized_df = df.copy()

        for column in df.columns:
            col_type = df[column].dtype

            if col_type != 'object':
                # Optimize numeric columns
                col_min = df[column].min()
                col_max = df[column].max()

                if col_type == 'int64':
                    if col_min > np.iinfo(np.int8).min and col_max < np.iinfo(np.int8).max:
                        optimized_df[column] = df[column].astype(np.int8)
                    elif col_min > np.iinfo(np.int16).min and col_max < np.iinfo(np.int16).max:
                        optimized_df[column] = df[column].astype(np.int16)
                    elif col_min > np.iinfo(np.int32).min and col_max < np.iinfo(np.int32).max:
                        optimized_df[column] = df[column].astype(np.int32)

                elif col_type == 'float64':
                    if col_min > np.finfo(np.float32).min and col_max < np.finfo(np.float32).max:
                        optimized_df[column] = df[column].astype(np.float32)

            else:
                # Optimize object columns (strings)
                num_unique_values = len(df[column].unique())
                num_total_values = len(df[column])

                if num_unique_values / num_total_values < 0.5:
                    optimized_df[column] = df[column].astype('category')

        return optimized_df

    @staticmethod
    def efficient_groupby_operations(df: pd.DataFrame) -> pd.DataFrame:
        """Demonstrate efficient groupby patterns"""

        # Slow: Multiple separate groupby operations
        # result1 = df.groupby('category')['value'].mean()
        # result2 = df.groupby('category')['value'].sum()
        # result3 = df.groupby('category')['value'].count()

        # Fast: Single groupby with agg
        result = df.groupby('category')['value'].agg(['mean', 'sum', 'count'])

        # Even faster for multiple columns
        multi_result = df.groupby('category').agg({
            'value1': ['mean', 'sum'],
            'value2': ['max', 'min'],
            'value3': 'count'
        })

        return result, multi_result

    @staticmethod
    def vectorized_string_operations(df: pd.DataFrame, column: str) -> pd.DataFrame:
        """Use vectorized string operations instead of apply"""

        # Slow: apply with lambda
        # df['processed'] = df[column].apply(lambda x: x.upper().replace(' ', '_'))

        # Fast: vectorized string operations
        df['processed'] = df[column].str.upper().str.replace(' ', '_', regex=False)

        # Complex string processing
        df['cleaned'] = (df[column]
                        .str.strip()
                        .str.lower()
                        .str.replace(r'[^\w\s]', '', regex=True)
                        .str.replace(r'\s+', ' ', regex=True))

        return df

    @staticmethod
    def efficient_merge_operations(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
        """Optimize DataFrame merge operations"""

        # Set index for faster merges if doing multiple merges on same keys
        df1_indexed = df1.set_index('key_column')
        df2_indexed = df2.set_index('key_column')

        # Fast merge using indices
        result = df1_indexed.join(df2_indexed, how='inner')

        # For large datasets, consider using merge with sorted data
        df1_sorted = df1.sort_values('key_column')
        df2_sorted = df2.sort_values('key_column')

        result_sorted = pd.merge(df1_sorted, df2_sorted, on='key_column', how='inner')

        return result

# Performance testing for pandas operations
def pandas_performance_comparison():
    """Compare different pandas operation approaches"""

    # Create test data
    np.random.seed(42)
    n_rows = 100000

    df = pd.DataFrame({
        'column1': np.random.randint(0, 200, n_rows),
        'column2': np.random.randint(0, 100, n_rows),
        'column3': np.random.choice(['target', 'other1', 'other2'], n_rows),
        'value': np.random.randn(n_rows)
    })

    # Test filtering performance
    import time

    print("Pandas Performance Comparison:")
    print("-" * 40)

    # Test slow method
    start_time = time.perf_counter()
    slow_result = slow_pandas_filtering(df)
    slow_time = time.perf_counter() - start_time

    # Test fast method
    start_time = time.perf_counter()
    fast_result = fast_pandas_filtering(df)
    fast_time = time.perf_counter() - start_time

    # Test fastest method
    start_time = time.perf_counter()
    fastest_result = even_faster_pandas_filtering(df)
    fastest_time = time.perf_counter() - start_time

    print(f"Slow method (zip):     {slow_time:.4f}s")
    print(f"Fast method (&, |):    {fast_time:.4f}s ({slow_time/fast_time:.1f}x faster)")
    print(f"Fastest method (.loc): {fastest_time:.4f}s ({slow_time/fastest_time:.1f}x faster)")

    # Memory optimization test
    original_memory = df.memory_usage(deep=True).sum()
    optimized_df = PandasOptimizer.optimize_dtypes(df)
    optimized_memory = optimized_df.memory_usage(deep=True).sum()

    print(f"\nMemory optimization:")
    print(f"Original:  {original_memory / 1024 / 1024:.2f} MB")
    print(f"Optimized: {optimized_memory / 1024 / 1024:.2f} MB")
    print(f"Reduction: {(1 - optimized_memory/original_memory)*100:.1f}%")

This comprehensive exploration of Python performance optimization demonstrates that writing fast Python code requires understanding both language internals and appropriate tool selection for different types of computational tasks.

These performance optimization insights from my archive showcase the evolution from basic Python usage to advanced performance engineering, emphasizing the importance of profiling, measurement, and systematic optimization approaches.