all groups > sql server programming > october 2007 >
You're in the

sql server programming

group:

there seems to be some overhead between SQL Server 2005 and a CLR function written in C#. Why is this?


there seems to be some overhead between SQL Server 2005 and a CLR function written in C#. Why is this? DR
10/26/2007 9:25:14 PM
sql server programming: there seems to be some overhead between SQL Server 2005 and a CLR function
written in C#. Why is this?

I have a simple wraper around System.Diagnostics.Stopwatch.GetTimestamp() :

public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static long GetTimestampF()
{
return System.Diagnostics.Stopwatch.GetTimestamp();
}
};


but if i run this it is hardly accurate. the time of getting from sql server
to a CLR function seems to be relatively slow for nanosecond calculations:

DECLARE @before bigint
DECLARE @after bigint
SET @before = dbo.GetTimestampF()
-- do something
SET @after = dbo.GetTimestampF()
SELECT @after - @before as nanoseconds

is there nyway to elimitate this overhead so that dbo.GetTimestampF()
executes as fast for TSQL as it is to call GetTimestampF() in C#?

If i call my GetTimestampF() on c# it executes fast enough for the
nanosecond results to be accurate

Re: there seems to be some overhead between SQL Server 2005 and a CLR function written in C#. Why is this? Erland Sommarskog
10/27/2007 2:56:13 PM
DR (softwareengineer98037@yahoo.com) writes:
[quoted text, click to view]

No. T-SQL and CLR are if not different universes or galaxies at least
different solar systems, and the overhead for crossing that boundary
is higher than calling a C# function from another C# function.

It's also a truth that measuring the performance of something can affect
that performance, and this amplified even more the smaller the numbers
are. Heisenbergs principle on uncertainty is applicable here.

I ran some tests, and I did not see any apparent flaw. Of course, I did
try not to measure in nanoseconds, rather I looked at numbers in the
millisecond range. And compared to getdate() which has a resolution
3.33 ms, Diagnostics.Stopwatch seems to be a lot better.

Do you have any feeling for big in microseconds this overhead would be?

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: there seems to be some overhead between SQL Server 2005 and a CLR function written in C#. Why is this? DR
10/29/2007 12:19:15 PM
is there something like gettimestamp for nanosecond time test in TSQL?

[quoted text, click to view]

Re: there seems to be some overhead between SQL Server 2005 and a CLR function written in C#. Why is this? Erland Sommarskog
10/29/2007 4:29:15 PM
DR (softwareengineer98037@yahoo.com) writes:
[quoted text, click to view]

No. SQL 2008 introduces new data types for date and time, and you can now
store datetime and time values down to the precision of 100 ns. There are
also new functions to return these data types, sysdatetime() and
sysdatetimeoffset(). Unfortunately, though, they don't really have more
accuracy than the old getdate(). The values are not rounded down to a
threehundreths of a second, but it still seems impossible to get a
diference between two values below 13 ms.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button