'How can I debug emacs' slow startup time?

I recently got a new Retina MacBook Pro, with Mountain Lion. Unfortunately, emacs is taking forever to startup (around 5s) on my new computer. I tried installing the latest homebrew version of emacs, but the problem persists. I don't have a .emacs file, so I'm clueless as to what could be causing the slow startup.

What tools do I have at my disposal to debug where emacs is spending its time during startup?



Solution 1:[1]

For starters, run emacs -Q (which will start Emacs with no start-up files at all), and see whether that's still slow.

You may wish to read over the following, which explains all the various possible files which Emacs will look for by default:

C-hig (emacs) Init File RET

Solution 2:[2]

Changing hostname didn't do anything for me. What worked wonders for me seems counter intuitive, but now my emacs starts instantly from terminal. This is what I did

alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs --debug-init

However, it still takes a few seconds, sometime even 15 seconds, to start from the graphical interface. Weird!

Solution 3:[3]

Emacs should start up instantaneously in your setup

Mac build (from sources)

% time /Applications/Emacs.app/Contents/MacOS/Emacs --debug-init -eval '(kill-emacs)' -Q
/Applications/Emacs.app/Contents/MacOS/Emacs --debug-init -eval '(kill-emacs)  0.19s user 0.06s system 35% cpu 0.696 total

NS build downloaded from emacsformacosx.com

% time /Volumes/Emacs/Emacs.app/Contents/MacOS/Emacs --debug-init -eval '(kill-emacs)' -Q
/Volumes/Emacs/Emacs.app/Contents/MacOS/Emacs --debug-init -eval  -Q  0.17s user 0.08s system 35% cpu 0.691 total

I don't use homebrew for Emacs but it should be similar. Are you sure you're not loading non-standard packages some how?

Solution 4:[4]

There really is something totally broken with emacs. If nameservice collapses or routing is not fine, or your vpn tunnel is off, etc.

For us, sysadmins, starting vi is always an option:

# time emacs -debug-init -eval '(kill-emacs)' -Q

real    2m5.177s
user    0m0.029s
sys     0m0.024s

At this time the vpnc has died and caused some problems (routing and nameservice). Notice over 2 minutes!

After 30 years using emacs still do not understand why it really needs to resolve your hostname. For locking files there are better alternatives than that.

Managing hundreds/thousands of machines and VMs and setting individual host files is not really an answer. Manually it would take days/weeks and automatically generating. It really is as good as nothing.

Solution 5:[5]

Fixed by adding the local hostname and corresponding ip in /etc/hosts for me

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phils
Solution 2 René Höhle
Solution 3 event_jr
Solution 4 armatita
Solution 5 Manuel Rozier