I coded a test for what, in my opinion, is the worst case scenario - a repeating line is repeated over and over, and we replace the specified line with another line. As t/n aligns as n increases, the worst-case scenario seems empirical, as if it could be O(n) . But I really can't argue with the @NickolayOlshevsky post.
import time from matplotlib import pyplot as plt x=[10] while x[-1]<10**8: x.append(int(x[len(x)-1]*1.5)) y = [0]*len(x) nst = 'abcd' sst = 'abcd' for ix,i in enumerate(x): s = ''.join([nst]*i) t = time.time() s = s.replace(sst,'efgh') y[ix] = time.time()-t x = [a*len(nst) for a in x] %matplotlib inline fig, (ax1,ax2) = plt.subplots(2, sharex=True) fig.set_size_inches(8, 6) ax1.set_xscale('log') ax1.set_yscale('log') ax1.set_xlabel('n') ax1.set_ylabel('t') ax1.plot(x,y) ax2.set_xscale('log') ax2.set_yscale('log') ax2.set_xlabel('n') ax2.set_ylabel('t/n') ax2.plot(x,[a/b for a,b in zip(x,y)])

source share