Fastest way to convert a URI into a scheme relative URI (in python)
<p>In python, whats the <em>fastest</em> way to convert a url of the type <code>scheme://netloc/path;parameters?query#fragment</code> into a "scheme relative" or "protocol relative" URI?</p>

<hr>

<p>Currently, I'm doing a version of <a href="http://stackoverflow.com/a/21687728/4936905">this</a>, but was thinking there might be a more concise/faster way to accomplish this. The input always has <code>https://</code> or <code>http://</code> appended to it.</p>

<p>The fastest way that I could find is with <code>str.partition</code>:</p>

<pre><code>In [1]: url = 'https://netloc/path;parameters?query#fragment'

In [2]: %timeit url.partition('://')[2]
The slowest run took 6.70 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 251 ns per loop

In [3]: %timeit url.split('://', 1)[1]
The slowest run took 5.20 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 407 ns per loop

In [4]: %timeit url.replace('http', '', 1).replace('s://', '', 1)
The slowest run took 4.24 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 589 ns per loop
</code></pre>

<p>Since all you're doing is stripping off text ending in a fixed string, there doesn't seem to be much benefit in parsing the URL.</p>

python