@@ -196,20 +196,40 @@ private TimeSpan RoundDuration(TimeSpan duration)
196196
197197
198198 /// <summary>
199- /// Format duration. Handles time better than inbuilt, but isn't culture-specific
199+ /// Faster format then inbuilt or string builder. May need tweaking
200200 /// </summary>
201201 private static string FormatDuration ( TimeSpan time )
202- {
203- if ( time == TimeSpan . Zero )
204- return string . Empty ;
202+ {
203+ /*
204+ Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
205+ WithArray 3.733 s 0.0719 s 0.0855 s 1.00 180000.0000 112000.0000 2000.0000 1.08 GB
206+ WithStringBuilder 8.663 s 0.1297 s 0.1213 s 1.00 396000.0000 289000.0000 1000.0000 2.35 GB
207+ */
208+
209+ var chars = new char [ 9 ] ;
210+ var hours = time . Hours ;
211+ var minutes = time . Minutes ;
212+ var seconds = time . Seconds ;
213+
214+ if ( hours > 10 ) chars [ 0 ] = ( char ) ( '0' + hours / 10 ) ;
215+ if ( hours > 0 )
216+ {
217+ chars [ 1 ] = ( char ) ( '0' + hours % 10 ) ;
218+ chars [ 2 ] = 'h' ;
219+ }
205220
206- var sb = new StringBuilder ( ) ;
207- if ( time . Hours > 0 ) sb . Append ( $ "{ time . Hours } h") ;
221+ if ( minutes > 10 || hours > 0 ) chars [ 3 ] = ( char ) ( '0' + minutes / 10 ) ;
222+ if ( minutes > 0 || hours > 0 )
223+ {
224+ chars [ 4 ] = ( char ) ( '0' + minutes % 10 ) ;
225+ chars [ 5 ] = 'm' ;
226+ }
208227
209- sb . Append ( $ "{ time . Minutes } m") ;
210- sb . Append ( $ "{ time . Seconds } s") ;
228+ chars [ 6 ] = ( char ) ( '0' + seconds / 10 ) ;
229+ chars [ 7 ] = ( char ) ( '0' + seconds % 10 ) ;
230+ chars [ 8 ] = 's' ;
211231
212- return sb . ToString ( ) ;
232+ return new string ( chars ) . Trim ( ' \0 ' ) ;
213233 }
214234
215235 }
0 commit comments