I’ve known for a long time it’s better to save for retirement through an IRA or 401K. But I never knew how much better–I did the math so you don’t have to.

Of course, before you invest for retirement you should make sure you have enough money for your day-to-day expenses, have an emergency fund, and in most cases pay off high-yield debt. But if you have extra funds to save for retirement, the benefits are huge. As always, this article isn’t investment advice!

Before quantifying the benefits of IRAs/401Ks, it’s worth explaining conceptually why they are so good. If you invest outside of an IRA or 401K, you effectively get taxed 3 times:

  1. You pay tax on your income and you only invest what’s left over.
  2. You will likely buy investments with a dividend yield and then you get taxed on the dividends every year.
  3. You pay capital gains tax when you sell your investments.

If you invest inside an IRA or 401K, you only get taxed once:

  1. If it’s a traditional IRA, you pay no income tax on what’s invested.
  2. You pay no taxes on the dividends your investments throw off each year.
  3. You do pay tax at normal income tax rates when you sell your investments.

I built a toy model to calculate how much more money you’d have, after tax, investing through an IRA/401K vs. not. I assumed an income tax rate of 40% (including state taxes) and capital gains / qualified dividend tax rate of 25%. I also assumed only 6% investment appreciation each year, 4.3% capital appreciation and a 1.7% dividend yield, which is the current S&P 500 yield. For simplicity, I only looked at the appreciation of 1 year’s worth of investments made at ~30 years old and held for 40 years.

Of course, your marginal tax rates can be higher or lower than what’s assumed here, you can hold the investments for less or more time if you’re closer or farther from retirement, and the appreciation of the investments will almost certainly be different than what I’ve assumed. Still, I figured these were approximate estimates for at least some of the people reading this.

Here’s where I got: based on the assumptions laid out above, you’d have 41.6% more money after tax in an IRA/401K. For example, if you had $25K in pre-tax income and you invested it after income tax without an IRA, you’d end up with $108.9K in 40 years. If you invested it in an IRA, you’d end up with $154.3K.

Interestingly, holding the number of investment years constant, the percentage delta between non-IRA and IRA investing is very sensitive to the dividend yield as a percentage of returns and the capital gains tax rate. This is intuitive because in only the taxable account scenario do you pay taxes on dividends or capital gains. The price appreciation assumption matters way less and the income tax rate does not matter at all. This final fact makes sense because in both scenarios you are paying the same income tax rate, either before or after the money is invested: XY = YX.

Another interesting takeaway: I calculated what percentage of the difference in returns between the two scenarios is because of not having to pay capital gains vs. being able to compound the benefit of not having to pay taxes on your dividends each year. I was pretty surprised–with the assumptions mentioned above, it’s nearly 70% (capital gains) to 30% (dividends). That’s a lot for the dividends! Compound interest is not intuitive and there is a huge benefit to being able to compound the tax savings on your dividends each year. And, of course, the dividend benefit of non-taxable accounts becomes even more important if you decide to invest in, say, stocks with a high dividend yield or high-yield bonds.

Finally, there are other benefits of IRAs and 401Ks that are more difficult to quantify. If you think your income tax rate will be lower in retirement than now–a reasonable assumption because you will by definition not be working–your tax rate in withdrawing from the IRA will be lower than what’s assumed above, boosting your after-tax returns. That boosts the benefit of the IRA/401K even more. Also, if you’re lucky and have funds available to donate to charity in retirement that you don’t need for your living expenses, you can potentially pay no income taxes at all on at least part of your IRA–either on the amount invested through the IRA or when it’s withdrawn.

In general, if you’re saving for retirement, IRAs and 401Ks are a no-brainer. Uncle Sam’s giving you 40%+ more money if you use them.