Microsoft (MSFT -1.84%) and HP (HPQ -0.25%) shaped the PC market over the past few decades. Microsoft Windows became the most popular operating system for PCs, and HP defended its position as the world's top PC maker.

But over the past decade, both companies have struggled with the rise of mobile devices. Microsoft tried to stay relevant with Windows Phone, which led to its disastrous acquisition of Nokia's smartphone unit. HP struggled with botched product launches, leadership issues, and terrible acquisitions.

Figurines of a bull and a bear on a stock chart.

Image source: Getty Images.

The future looked bleak for Microsoft and HP, but both companies managed to reinvent themselves. Microsoft, under the guidance of CEO Satya Nadella, reduced its dependence on legacy software licenses and transformed itself into a cloud services company. It also breathed fresh life into the PC market with the Surface, which sparked a form factor revolution among OEMs.

HP split into two companies in late 2015, with its namesake company retaining the PC and printer units and Hewlett-Packard Enterprise taking over the enterprise hardware, software, and services businesses. Both Microsoft and the "new" HP thrived under their new business models.

Over the past 12 months, shares of Microsoft and HP rallied nearly 50% and 30%, respectively. But is either "mature" tech stock worth buying after those big gains? Let's take a closer look at both companies to find out.

How fast are Microsoft and HP growing?

Microsoft splits its sprawling business into three main segments -- More Personal Computing, which includes Windows, search, and hardware devices; Productivity and Business Processes, which include Office, Dynamics, and LinkedIn; and the Intelligent Cloud, which includes Windows Server, Azure, and other services.

Microsoft offsets the slower growth of the More Personal Computing segment with the stronger growth of its Productivity and Business Processes and Intelligent Cloud businesses. That's how it generated double-digit sales growth during the first three quarters of fiscal 2018, and why analysts expect its sales to rise 13% for the full year when it reports its full-year earnings on July 19.

An illustration of cloud network connections.

Image source: Getty Images.

HP has only two core businesses -- Personal Systems (PCs) and printers. Its PC business has consistently outgrown the overall market with well-received 2-in-1 devices and high-end laptops while stealing market share from rivals like Lenovo. Its printer business has also evolved and expanded, launching new mobile printers and industrial 3D printers, and acquiring Samsung's printing business.

As a result, HP's PC and printer businesses defied expectations with robust sales growth, which helped generate double-digit sales growth over the past three quarters. Analysts expect HP's total revenues to rise 11% for the full year.

How profitable are Microsoft and HP?

Microsoft and HP are very different companies, so their margins and profitability aren't truly comparable. Microsoft mostly sells software, so it consistently posts higher margins than HP, which deals in lower-end hardware.

MSFT Gross Profit Margin (Quarterly) Chart

MSFT Gross Profit Margin (Quarterly) data by YCharts

However, we should note that many of HP's rivals struggle to maintain comparable margins and sales growth in the crowded PC and printing markets. Lenovo, for example,reported a gross margin of just 13.8% last year.

Rising operating expenses remain a minor concern at Microsoft since it needs to spend more money to expand its cloud and mobile services ecosystem with new products and marketing pushes. Yet that pressure is largely offset by the robust sales growth of key cloud services like Azure, Dynamics, and Office 365.

MSFT Operating Margin (Quarterly) Chart

MSFT Operating Margin (Quarterly) data by YCharts

Wall Street expects Microsoft's earnings to grow 17% this year, and for HP's earnings to rise 21%. Those are impressive growth rates for so-called "mature" tech companies. However, Microsoft currently trades at 26 times forward earnings, compared to HP's much lower P/E of 11.

That difference indicates that investors view Microsoft as a high-growth "cloud" stock, and HP as an older hardware maker. However, the numbers suggest that HP's stock is cheaper relative to its earnings growth potential than Microsoft. HP's forward dividend yield of 2.4% also tops Microsoft's 1.7% yield.

The winner: HP

Microsoft's comeback under Nadella has been incredible, and will likely persist for years to come. However, HP has stronger earnings growth potential, its stock is cheaper, and its dividend is higher. So if I had to choose one over the other, I'd stick with HP.