If you had invested in Microsoft (NASDAQ:MSFT) and Google -- now part of Alphabet (NASDAQ:GOOG) (NASDAQ:GOOGL) -- three years ago, your return on investment would be about the same. Microsoft's shares have climbed 119% as of this writing, and Google/Alphabet shares are up 105%. Those are impressive gains on their own, but they're even better when you consider that the S&P 500 gained just 30% over the same period.

Share-price gains aside, both of these companies are still dominating the tech space. But which company looks like the better long-term buy right now? Let's take a look at the companies' financial fortitude, competitive advantages, and valuations to help answer that question.

People standing over a conference table pointing to charts

Image source: Getty Images.

Financial fortitude

Company

Cash

Debt

Free Cash Flow (TTM)

Microsoft

$132.2 billion

$88.6 billion

$33.5 billion

Alphabet

$102.8 billion

$5.3 billion

$21.2 billion

Data sources: Yahoo! Finance and Morningstar. TTM = trailing-12-month.

Each of these companies has an impressive amount of free cash flow and a massive amount of cash. But Alphabet has significantly less debt, with just over $5 billion, while Microsoft has amassed far more. That doesn't mean that Microsoft has any problems covering its debt responsibilities, but it does mean that Alphabet gets the win in this category.

Winner: Alphabet.

Competitive advantage

Microsoft built its dominance with its Office suite of applications and its Windows operating system, but lately the company has focused its attention on becoming a cloud computing powerhouse.

Several years ago Microsoft CEO Satya Nadella set a goal for his company to achieve an annual run rate of $20 billion for cloud computing sales by the end of fiscal 2018. Microsoft achieved the goal in the first quarter of the fiscal year, several quarters ahead of schedule.

Much of the company's progress comes from its Azure cloud computing services, which saw sales jump by 93% in the most recent quarter. That's great news, as the cloud computing market is expected to grow from $285 billion last year to $411 billion by 2020.

But even with all of this growth from Microsoft's cloud computing efforts, the company still falls far behind the current leader. Amazon held 33% of the cloud infrastructure market in the first quarter, leaving Microsoft in the No. 2 spot with just 13%. So while cloud computing continues to offer new areas of growth for Microsoft, the company doesn't really have a competitive advantage in the space right now.

In contrast to Microsoft, Alphabet has some significant advantages over its competitors in a few key areas. First, and most importantly, Google holds a massive 37% of the digital ad market, compared to its rival Facebook's 19%.

According to data by eMarketer, Google will hold onto 36% or more of the market until at least 2020. This leading position in the ad space earned Google $26.6 billion in the most recent quarter, an increase of 24% year over year.

But Alphabet's competitive leads go beyond just the advertising space. Consider that Waymo, a driverless car company spun out of Google, has already taken the lead in the autonomous vehicle space. Waymo has plans to launch a self-driving fleet of taxis later this year, which would be the first ever of its kind.

Sure, Waymo isn't a significant source of revenue for the company, but with the autonomous vehicle market expected to reach $127 billion by 2027, Alphabet's advantage starts to become clearer.

Additionally, Google has been busy gobbling up artificial intelligence (AI) companies and even building its own AI processor to set itself apart. The company uses AI for everything from its cloud computing platform to software for Waymo, and even to improve its advertising business. With the company's early moves in AI, and its ability to use it across many of its businesses, Alphabet is building out a key position in this growing market as well.

Alphabet simply has a bigger lead in its key advertising business than Microsoft does with its cloud computing focus, as well as other growing advantages over its competitors. So I'm giving Alphabet the win here.

Winner: Alphabet.

Valuation

Now let's take a quick look at both of these companies' valuations based on their price-to-earnings ratios (P/E) and their forward P/Es (a ratio that looks at future earnings projections).

Company

P/E Ratio (TTM)

Forward P/E

Microsoft

67.0

24.6

Alphabet

47.4

23.6

Data source: Yahoo Finance. TTM = trailing-12-month. Alphabet data is for Class C (GOOG) shares.

Technology stocks often have high price-to-earnings ratios relative to other industries and the broader S&P 500, which has an average P/E of about 25 right now.

Surprisingly, Microsoft and Alphabet's shares currently trade at about that average when factoring in projected earnings. But when we look back at Microsoft's trailing P/E we see that it's significantly higher than Alphabet's, making its shares relatively more expensive.

Microsoft's stock isn't absurdly overpriced, but it does appear more expensive than Alphabet's shares, so Alphabet gets the win for this category.

Winner: Alphabet.

The verdict

Based on all of the factors above, Alphabet is the better buy right now. I still believe Microsoft is a good bet for many investors. But when you compare Alphabet's dominance in advertising and other businesses, along with the company's strong financial performance and its relatively inexpensive share price, Alphabet is the clear winner.

John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. Suzanne Frey, an executive at Alphabet, is a member of The Motley Fool's board of directors. Teresa Kersten is an employee of LinkedIn and is a member of The Motley Fool's board of directors; LinkedIn is owned by Microsoft. Chris Neiger has no position in any of the stocks mentioned. The Motley Fool owns shares of and recommends Alphabet (A shares), Alphabet (C shares), AMZN, and FB. The Motley Fool has a disclosure policy.