Hi all, thank you for all of the suggestions.
I am quite happy with the speed of the system now.
I made the following changes:
1. In lib/navigationlib.php I removed enrol_add_course_navigation($coursenode, $course); (around line 3665 in load_course_settings). This improved the average page creation time from 7 seconds to 1 second.
2. I disabled the Activity names auto-linking filter. Now after clicking "Attempt Quiz" the quiz is made in about 5-6 seconds instead of 25+ seconds.