[Mono-list] File LastModified

José Alexandre Antunes Faria spigaz at gmail.com
Wed Apr 4 20:05:03 EDT 2007


Hi there guys,

One of my apps uses a great deal of files and check their lastmodified
timestamps, as I guess many other do.

When I was profiling it, I noticed that it was taking a great deal of
time, getting the lastmodified attribute, so I gave it a look.

And I found out that internally mono uses UTC time, meaning the libs
always get the LastWriteTime in UTC. Then they convert it to LocalTime
before any usage.

Meaning that if the required is UTC, its converted back to UTC. That is
two unnecessary conversions.

I have run some tests (100000000 times getting the LastWriteTimeUtc),
and have found out, that it wastes about 10% converting back to
LocalTime. 

I don't have a number on the conversion from utc to localtime, but I
seen that you use some tricks on the DateTime.Now, so I would guess that
it isn't that light either.

I know that an optimization of 20% is close to nothing on things are are
very little used, like file manipulation.

I don't know if this was intentional ou its simply a mistake, as it in
fact allows to simplify the implementation, but why isn't utc used
instead of local?

The same happens with the other GetLast*Time and SetLast*.

I haven't proposed a patch, because I don't know it makes sense to fix
this or not.

I would appreciate your comment on this.

Thanks,

SpigaZ out.


PS: Here are some snippets of the code in question.

//// File.cs:


public static DateTime GetLastWriteTime (string path)
{
 MonoIOStat stat;
 MonoIOError error;
 CheckPathExceptions (path);

 if (!MonoIO.GetFileStat (path, out stat, out error)) {
#if NET_2_0
  if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error ==
MonoIOError.ERROR_FILE_NOT_FOUND)
   return _defaultLocalFileTime;
  else
   throw new IOException (path);
#else
  throw new IOException (path);
#endif
 }
 return DateTime.FromFileTime (stat.LastWriteTime);
}

public static DateTime GetLastWriteTimeUtc (string path)
{
 return GetLastWriteTime (path).ToUniversalTime ();
}


// DateTime.cs
  
public static DateTime FromFileTime (long fileTime) 
{
 if (fileTime < 0)
  throw new ArgumentOutOfRangeException ("fileTime", "< 0");

 return new DateTime (w32file_epoch + fileTime).ToLocalTime ();
}

#if NET_1_1
public static DateTime FromFileTimeUtc (long fileTime) 
{
 if (fileTime < 0)
  throw new ArgumentOutOfRangeException ("fileTime", "< 0");

 return new DateTime (w32file_epoch + fileTime);
}
#endif



More information about the Mono-list mailing list