Repository: yt-dlp
Issue: Issues and suggestions related to performance
Labels: site-enhancement,triage

### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I remove or skip any mandatory\* field

### Checklist

- [X] I'm requesting a feature unrelated to a specific site
- [X] I've looked through the [README](https://github.com/yt-dlp/yt-dlp#readme)
- [X] I've verified that I'm running yt-dlp version **2022.11.11** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [X] I've searched the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)

### Provide a description that is worded well enough to be understood

Getting the final URL for sites usually takes several seconds, whereas the same can often be accomplished with a small, customized script in a fraction of the time. So I did some benchmarking to look into this. See results below.

Questions:

1. Are there plans to optimize away the general overhead that adds seconds to the performance?
2. Why are multiple m3u8/MPD files loaded? Can this be optimized? Is there a setting to get only one?
3. To put focus on performance, would it be possible to add a CLI option to time program execution (like behow)?

Thanks in advance for your kind response and for making this improved and well-maintained fork!

```
$ sudo purge && yt-dlp -v -g https://www.svtplay.se/video/8MMzVRJ/en-underbar-javla-jul
0.08 0.08 [debug] Command-line config: ['-v', '-g', 'https://www.svtplay.se/video/8MMzVRJ/en-underbar-javla-jul']
---> WHAT TAKES A SECOND HERE?
0.92 1.00 [debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
0.00 1.00 [debug] yt-dlp version 2022.11.11 [8b644025b] (source)
0.00 1.00 [debug] Lazy loading extractors is disabled
0.00 1.00 [debug] Plugins: ['SamplePluginIE', 'SamplePluginPP']
0.09 1.09 [debug] Git HEAD: 5424dba
0.07 1.16 [debug] Python 3.10.9 (CPython x86_64 64bit) - macOS-12.6.2-x86_64-i386-64bit (OpenSSL 1.1.1s  1 Nov 2022)
---> WHAT TAKES A SECOND HERE?
0.93 2.09 [debug] exe versions: ffmpeg git-2022-06-01-c6364b71 (setts), ffprobe git-2022-06-01-c6364b71, rtmpdump 2.4
0.00 2.10 [debug] Optional libraries: Cryptodome-3.15.0, certifi-2022.06.15, sqlite3-2.6.0, xattr-0.9.9
0.03 2.12 [debug] Proxy map: {}
0.08 2.20 [debug] Loaded 1736 extractors
---> WHAT TAKES HALF A SECOND HERE?
0.64 2.84 [debug] Using fake IP 78.72.0.76 (SE) as X-Forwarded-For
0.00 2.84 [SVTPlay] Extracting URL: https://www.svtplay.se/video/8MMzVRJ/en-underbar-javla-jul
0.00 2.84 [SVTPlay] 8MMzVRJ: Downloading webpage
0.11 2.95 [SVTPlay] 8MMzVRJ: Downloading JSON metadata
---> WHY ARE ALL THESE LOADED?
0.02 2.98 [SVTPlay] 8MMzVRJ: Downloading MPD manifest
0.18 3.15 [SVTPlay] 8MMzVRJ: Downloading m3u8 information
0.14 3.30 [SVTPlay] 8MMzVRJ: Downloading m3u8 information
0.14 3.44 [SVTPlay] 8MMzVRJ: Downloading MPD manifest
0.19 3.63 [SVTPlay] 8MMzVRJ: Downloading MPD manifest
0.17 3.80 [SVTPlay] 8MMzVRJ: Downloading m3u8 information
0.14 3.95 [SVTPlay] 8MMzVRJ: Downloading MPD manifest
0.21 4.15 [SVTPlay] 8MMzVRJ: Downloading m3u8 information
0.14 4.29 [SVTPlay] 8MMzVRJ: Downloading m3u8 information
0.14 4.44 [SVTPlay] 8MMzVRJ: Downloading m3u8 information
0.15 4.59 [SVTPlay] 8MMzVRJ: Downloading MPD manifest
0.17 4.75 [SVTPlay] 8MMzVRJ: Downloading MPD manifest
0.19 4.95 [debug] Formats sorted by: hasvid, ie_pref, lang, quality, res, fps, hdr:12(7), vcodec:vp9.2(10), channels, acodec, filesize, fs_approx, tbr, vbr, abr, asr, proto, vext, aext, hasaud, source, id
0.13 5.08 [debug] Default format spec: bestvideo*+bestaudio/best
0.01 5.08 [info] 8MMzVRJ: Downloading 1 format(s): dash-hbbtv-hevc-0+dash-full-13
0.06 5.14 https://svt-vod-6m.akamaized.net/d0/world/20221202/ec9513e7-343e-4af0-93f7-cad3ee8e679e/dash-hbbtv-hevc.mpd
```

Crude benchmarking code used:

```
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py
index 65408bf..cc9c3f1 100644
--- a/yt_dlp/utils.py
+++ b/yt_dlp/utils.py
@@ -2037,2 +2037,6 @@ def get_windows_version():
 
+import time
+start_time = time.time()
+last_time = start_time
+
 def write_string(s, out=None, encoding=None):
@@ -2051,2 +2055,7 @@ def write_string(s, out=None, encoding=None):
 
+    global last_time
+    curr_time = time.time()
+    diff = curr_time - last_time
+    last_time = curr_time
+    s = "\033[101m" if diff >= 0.4 else "\033[2m" + "{:.2f}\033[m \033[93m{:.2f}\033[m".format(diff, curr_time-start_time) + ' ' + s
     buffer.write(s.encode(enc, 'ignore') if enc else s)
```

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

_No response_