Python Benchmark: Replace in string

Looking for a fast way to "Replace in string" in python? Check below to see see which method is the fastest and uses less memory.

Packages

Setup

Runs before the benchmark, and it is not measured. Good place to import libraries and prepare data. Learn more

Code 1

Code 2

Code 3

Teardown

Runs after the benchmark, and it is not measured. Good place to print out results. Learn more

Results

Fastest

str.replace

1.83 us

547K op/s

Speed

BenchmarkRuntime
Operations
VS
Visual
str.replace1.83 us547K op/s0.0000018272345501868034
re.sub compiled2.58 us388K op/s1.41×0.0000025766104711334018
re.sub6.13 us163K op/s3.4×0.0000061339529533614315

Memory

BenchmarkPeak Memory
Score
VS
Visual
re.sub compiled< 1 KB13.60
re.sub< 1 KB13.20
str.replace< 1 KB13.70

Output

str.replace
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
re.sub
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
re.sub compiled
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.
    
    Nature and urban life exist in harmony in Brisbane, 
    on Australia's west coast. Here, where the locals 
    soak up more sunny days than in any other Australian 
    capital city, you can visit nearby Rottnest Island, 
    walk in leafy Kings Park and experience Swan Valley's 
    vineyards, Aboriginal art, food and history. 
    For local culture, wander the nearby city of 
    Fremantle's winding portside streets, Brisbane city 
    centre's museum and gallery precinct or peel back 
    the layers of this urban landscape with an Aboriginal guide.