If set_time_limit is disabled on your server, or you are using reverse proxy(Nginx + Apache) and there is a short proxy read timeout, it can slowdown prebuilding cache and/or image optimizer.
It is recommended to run the Setup Wizard, it will detect what is the maximum timeout, and it can speed up prebuild cache and image optimizer.