MONTREAL -- Roger Federer isn't one to gloat over his stunning record against former top-10 opponent David Ferrer.

Without playing especially well, the 19-time grand slam champion from Switzerland defeated Ferrer 4-6, 6-4, 6-2 in the third round of the Rogers Cup on Thursday, stretching his career record against the Spaniard to 17-0. It started with a win in Vienna in 2003.

"Maybe in the beginning he was not as good as he is now," said Federer. "Maybe I won five times because I'm better than he was.

"I was No. 1 in the world. I played him on hard courts also. I didn't play him often on clay. Also, there were many tight matches, so maybe it became a mental thing for him. I have a lot of respect for David. As a person, he's very nice. He's a great fighter on the court. So this type of head-to-head is a bit strange."

In Friday's quarter-finals, second-seeded Federer will face 12th-seeded Roberto Bautista Agut of Spain, against whom he is 6-0. Bautista Agut outlasted Frenchman Gael Monfils 4-6, 7-6 (5), 7-6 (2) in a two hours 56 minutes battle on centre court at Uniprix Stadium.

Unseeded Argentine Diego Schwartzman posted a strange win over American Jared Donaldson 0-6, 7-5, 7-5 to advance to a quarter-final meeting with Robin Haase, the 52nd-ranked Dutchman who upset seventh-seeded Grigor Dimitrov of Bulgaria 7-6 (3), 4-6, 6-1.

Kevin Anderson of South Africa downed American Sam Querrey 6-4, 6-1 and will next play the winner between fourth-seeded Alexander Zverev and 16th seeded Nick Kyrgios.

Federer, who breezed past Canadian Peter Polansky in the second round on Wednesday, looked lost in the opening set, spraying balls long, wide or into the net, but gradually rediscovered at least some of the form that has seen the 36-year-old Swiss put back the clock with two grand slam wins this year.

It was only the seventh time he lost a set to Ferrer, who was ranked third in the world in 2013.

"It's only normal to lose sets and lose matches," said Federer. "You can't win every set, every match you play out there.

"I like to expose myself to those kind of matches. I actually feel really happy because I know I can play a lot better. David can play a lot better, too. We battled. Both tried to find a way to win. He had a good start, I had a better finish. So take it how it is and hope that this match gives me some better rhythm and confidence against Bautista Agut, who plays actually very similar to David."

Ferrer has not beat a top-10 opponent in his last 13 attempts.

Bautista Agut fought off a match point to force a tiebreaker, which the tired-looking Monfils opened with a double fault and never challenged again.

The unseeded Monfils played his third straight three-set match, including an upset win over fifth-seeded Kei Nishikori in the second round.

Schwartzman saved four match points to upset third-seeded Dominic Thiem in the second round. The win over Donaldson put him in a quarter-final for the sixth time this year. Schwartzman's only ATP win was on clay in Istanbul last year.

Donaldson, 20, was seeking a first career quarter-final in a Masters Series tournament, where he is 0-13 in round of 16 matches.

Later Thursday, Canadian Denis Shapovalov faced top seed Rafael Nadal.