Written by

Senior Cloud Architect at InterSystems
Question Eduard Lebedyuk · Aug 31, 2022

Is there a simplier way to get posix time with fractional seconds?

Currently what I have is:

set now = $now()
set ts = $zdt(now,-2) _ "." _ $p(now, ".", 2)

Docs state (for -2 dformat):

Fractional seconds in the input value are permitted, but ignored.
 
I wonder if there's some other way to get posix time with fractional seconds?

Comments

Tomohiro Iwamoto · Aug 31, 2022

How about this? :-)

USER>w##class(%SYS.Python).Import("time").time()
1661966455.799232244
0
Herman Slagman  Sep 1, 2022 to Tomohiro Iwamoto

Except that calling Python is about 10x slower, although it gives probably the value that you want.
$Now() gives a different value because it doesn't take Daylight Savings Time into account.
 

0
Eduard Lebedyuk  Sep 1, 2022 to Herman Slagman

Except that calling Python is about 10x slower, a

Not really, more like faster if you need to call it more than once:

 

Code

Class Utils.Time
{

/// do ##class(Utils.Time).Test()ClassMethod Test()
{
	do..Main(1)
	do..Main(1)
	do..Main(1)
	do..Main(2)
	do..Main(3)
	do..Main(5)
	do..Main(10)
	do..Main(100)
	do..Main(1000)
	do..Main(10000)
}

/// do ##class(Utils.Time).Main(100)ClassMethod Main(count = 100)
{
	set od=$ioset nul="\\.\nul"// /dev/null/ - UNIXopen nul
	use nul
	
	s startA = $NOW()
	do..JobA(count)
	s endA = $NOW()
	s timeA = $p(endA,",",*) - $p(startA,",",*)
	
	s startB = $NOW()
	do..JobB(count)
	s endB = $NOW()
	s timeB = $p(endB,",",*) - $p(startB,",",*)
	
	use od
	close nul
	
	w"Iterations: ",count,!
	w"Time JobA: ",timeA,!
	w"Time JobB: ",timeB,!
	w"JobA takes ",$FN(timeA/timeB*100,"",2) _ "% of JobB time",!,!
}

ClassMethod JobA(count = 100) As%Status
{
	for i=1:1:count {
		set now = $now()
		set ts = $zdt(now,-2) _ "." _ $p(now, ".", 2)
	}
}

ClassMethod JobB(count = 100) As%Status
{
	set time = ##class(%SYS.Python).Import("time")
	for i=1:1:count {	
		set ts = time.time()
	}
}
}

Results in:

 

Output

Iterations: 1
Time JobA: .0000257
Time JobB: .0000728
JobA takes 35.30% of JobB time
 
Iterations: 1
Time JobA: .0000154
Time JobB: .0000206
JobA takes 74.76% of JobB time
 
Iterations: 1
Time JobA: .0000157
Time JobB: .0000158
JobA takes 99.37% of JobB time
 
Iterations: 2
Time JobA: .0000214
Time JobB: .0000161
JobA takes 132.92% of JobB time
 
Iterations: 3
Time JobA: .0000264
Time JobB: .0000158
JobA takes 167.09% of JobB time
 
Iterations: 5
Time JobA: .0000528
Time JobB: .0000258
JobA takes 204.65% of JobB time
 
Iterations: 10
Time JobA: .0000987
Time JobB: .0000256
JobA takes 385.55% of JobB time
 
Iterations: 100
Time JobA: .0007513
Time JobB: .0000606
JobA takes 1239.77% of JobB time
 
Iterations: 1000
Time JobA: .0057593
Time JobB: .0002278
JobA takes 2528.23% of JobB time
 
Iterations: 10000
Time JobA: .0535617
Time JobB: .0036764
JobA takes 1456.91% of JobB time
0