There are actually several options. Which works best for you will depend on your work-flow and document structure, as well as your preferences. If, for example, you also have non-TikZ/PGF images to compile, a more generic solution may be preferable. If all of your code is inline and your only concern is TikZ/PGF, then a TikZ/PGF-specific solution may work better.
external TikZ library
This works whether your pictures' code is provided inline or in supplementary files.
It is limited to TikZ/PGF pictures e.g. tikzpicture or environments such as forest which are based on tikzpicture or \tikz.
Add the following to your preamble.
\usetikzlibrary{external}
\tikzexternalenable
Then compile with shell escape enabled. For example, use pdflatex --shell-escape rather than pdflatex. The precise option you need may differ, but the idea is that you need to switch off the security restrictions which usually limit TeX's ability to write to arbitrary files.
The first time you compile, the compilation will actually take longer. However, subsequent compilations will be faster. By default, externalisation will include an image rather than compiling the picture code provided
- the code for the
tikzpicture has not changed;
- a compiled version of the image exists;
- it can i.e. shell escape is enabled etc.
Some constructions do not take kindly to externalisation. In this case, you can switch it off for those pictures. There are several ways to do this. One is to use
\tikzexternaldisable
and
\tikzexternalenable
to switch it back on.
If you have pictures which use pgfplots or forest, for example, then note that those packages support externalisation via their own options and you should use the appropriate option or package library to enable this.
Externalisation must be switched off for pictures which require overlay or remember picture. For example, you must switch it off for code which uses the tikzmark library or similar.
standalone
This solution is not limited to TikZ/PGF pictures but can also be used for images created using other packages.
It requires you to put the code for your pictures into external files which are then \input into the main document.
By default, standalone compiles the code for each picture each time. However, you may alter its mode so that it instead compiles external images which can then be included on subsequent compilations.