Performance of CAST vs CONVERT
This post is a follow-up to my prior post inspecting the performance of PARSE vs CAST & CONVERT, where we see that PARSE
is an order of magnitude slower than CONVERT
. In this post, we’ll check if there is a similar difference between using CAST
or CONVERT
. But just to be clear, CONVERT
offers a lot more functionality than CAST
; this post will not help you decide which of these functions to use for a specific use-case – I leave that to the reader to decide for themselves.
Minimal, Complete, and Verifiable Example
The code below is borrowed from the post on PARSE vs CAST & CONVERT, except here we’re inserting 1,000,000 rows into the dbo.ParseTest
table since CAST and CONVERT are so much faster than PARSE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
USE tempdb; DROP TABLE IF EXISTS dbo.ParseTest; DROP TABLE IF EXISTS dbo.ParseTestOutput1; DROP TABLE IF EXISTS dbo.ParseTestOutput2; GO CREATE TABLE dbo.ParseTest ( d varchar(23) NOT NULL ); INSERT INTO dbo.ParseTest(d) SELECT TOP(1000000) DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1, '2019-08-20 00:00:00') FROM sys.syscolumns sc1 CROSS JOIN sys.syscolumns sc2; CREATE TABLE dbo.ParseTestOutput1 ( d datetime NOT NULL ); CREATE TABLE dbo.ParseTestOutput2 ( d datetime NOT NULL ); SELECT * FROM dbo.ParseTest; GO |
The last statement simply pulls data from the dbo.ParseTest
table into memory; if your server is low on memory you may want to reduce the number of rows for your testing. On my test server, the code above took approximately 20 seconds to insert the rows, return them to SSMS, and render them to the screen.
The code below compares the performance of CAST
to CONVERT
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SET STATISTICS IO, TIME ON; GO INSERT INTO dbo.ParseTestOutput1 (d) SELECT CAST(pt.d AS datetime) FROM dbo.ParseTest pt; GO INSERT INTO dbo.ParseTestOutput2 (d) SELECT CONVERT(datetime, pt.d) FROM dbo.ParseTest pt; GO SET STATISTICS IO, TIME OFF; |
Notice I’m not specifying a specific type of output for the CONVERT
function; this is simply to get as close to an apples-to-apples comparison as possible. The Messages tab in SSMS shows the following results for our little test:
As you can see from the output above, CAST
is very slightly quicker. If you re-run this test code many times, you’ll see results that vary slightly. Below, I show the CPU and Elapsed times for 10 runs of the test code above. I’ve broken out the times by function to make it easier to compare the two:
CAST:
CONVERT:
Average CPU time for CAST
is 6026.5 milliseconds. Average CPU time for CONVERT
is 6050.1 milliseconds. That’s less than 1% difference between the two.
Summary
It’s safe to assume the performance differences between CAST
and CONVERT
are negligible, for the case presented above, where we’re converting a character-based date value into a datetime
value. In future, I’ll check out using CAST
and CONVERT
when converting datetime
values into various forms of character-based date and time values.
Thanks for reading this post – if you like it, hit the “subscribe” button to get on our mailing list. That way, you’ll be the first to know when we release new blog posts!
Check out the rest of our posts on performance.
[…] Hannah Vernon runs a performance test of CAST versus CONVERT: […]