Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Time in Milliseconds or NanoSeconds

by ramya2005 (Scribe)
on Aug 11, 2005 at 23:43 UTC ( #483151=perlquestion: print w/replies, xml ) Need Help??
ramya2005 has asked for the wisdom of the Perl Monks concerning the following question:


I do not want to create a temporary file.
I will give a more specific requirement.
I have to take a value from the XML file and change it into a unique text.
So what I am doing is appending the original text with process ID and Timestamp as I mentioned before.
If using timestamp is not appropriate please suggest me a more reliable solution.
------ From Prev Thread -----------------------------------
I have to create unique file names in a multiuser environment. So I am doing usertext_processID_TimeStamp to create unique file names. The following code is giving me the timestamp is seconds.
$unique_name = $user_name."_"."$$"."_"."$^T";
There are instances where two names are created in the same second and they are alike.
Is there a perlish way of getting time in nano seconds or milli seconds (or) I have to convert seconds to milliseconds manually?
Also let me know if there is a better way of creating unique names.

Thank you.

Replies are listed 'Best First'.
Re: Time in Milliseconds or NanoSeconds
by merlyn (Sage) on Aug 11, 2005 at 23:45 UTC
Re: Time in Milliseconds or NanoSeconds
by Zaxo (Archbishop) on Aug 11, 2005 at 23:48 UTC

    Time::HiRes gives high precision time, though nanosecond resolution is unlikely.

    The better idea is File::Temp. Timestamps are notoriously unreliable for your need.

    After Compline,
    Zaxo

Re: Time in Milliseconds or NanoSeconds
by sk (Curate) on Aug 12, 2005 at 01:21 UTC
    You mention that you do not want to create a temporary file but the file you are creating is not that clean either.

    Note that File::Temp does not have to "delete" the file. Is that why you were hesistating to use temp files? You can set the behavior using this method   $fh->unlink_on_destroy( 0 );

    Do you want the username prefix or is it ok if you don't have that?

    -SK


      I mention I do not want to create temporary file because what I am dealing with is a text (one of the elements in XML).
      For example say I have an XML file "Filters.xml".
      <filterobj> <filter name="abc"/> <filter name="cde"/> </filterobj>
      My requirement is to read this XML and do the following.
      Convert 'abc' into 'abc.processID.TimeStamp' Convert 'cde' into 'cde.ProcessID.TimeStamp'
      Update the above XML file and Update another XML file which is referring to 'abc' and 'cde'.

      So there is no temp file concept. I have to create unique names to use in the same file.

      The problem now is I am using timestamp in seconds and there could be more than one name generated in the same second with same user provided name. So I asked for some input in creating better unique names and I asked Perl monger's comments on using milli seconds.
      Thanks
        The problem now is I am using timestamp in seconds and there could be more than one name generated in the same second with same user provided name.

        How about using a simple incrementing number in this case? You can use sysopen to do this and avoid race conditions:

        use Fcntl; my $username = "..."; my $filename = $username . time; my $inc = 1; my $fh; until (sysopen $fh, $filename.$inc, O_WRONLY|O_CREAT|O_EXCL) { $inc++; } print $fh time, "\n";
      More the the point, File::Temp has a function (as Zaxo alludes to) that can generate names without creating an actual file. (The function in question is tmpnam.) Note, however, that uniqueness is not absolutely guaranteed, although it does to a very good job.
Re: Time in Milliseconds or NanoSeconds
by castaway (Parson) on Aug 12, 2005 at 07:04 UTC
    $^T is not a current timestamp, it is the time that your script started executing, thus it is just as unique as the process_id. For the current time, you want the time() call.

    C.

      "Just as unique" is imprecise. The real question is, what is the likelihood of collision in the two value spaces? Collision in pid space occurs when the pid rolls over (it's typically 15 bits) and an "old" process is still running. Collision in $^T space occurs when more than one process is started in the same clock second. For there to be a collision in the combined space, such as the OP had, you'd have to have a situation where more than 32k processes are started in the same second. That's a significantly better bet than if using either one alone.
Re: Time in Milliseconds or NanoSeconds
by DrAxeman (Scribe) on Aug 12, 2005 at 01:24 UTC
    Maybe this is an over-simplified look at it, but why not simply use rand() to append a new value to the file?

    Or is there a chance that you'd get still get duplicates? If not, this would be a very simple solution with little overhead.
      Or is there a chance that you'd get still get duplicates?
      Bingo. For truly random numbers, it's not unreasonable to get the same number 50 times in a row. Moreover, when you ask a computer for a random number, one of two things happens. Maybe it uses a pseudo-random number generator (PRNG), which has an inherent (albeit large) cycle, to get it. Or, maybe it has some way to get truly random numbers. But that falls into the first trap. To me, when someone wants uniqueness, UUID comes to mind. It's already been mentioned by another poster in this thread.

      HTH,
      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        Excellent point.
Unique stamp
by schwern (Scribe) on Aug 13, 2005 at 02:19 UTC
    So there is no temp file concept. I have to create unique names to use + in the same file.

    Process ID + timestamp + a counter.

    my $counter = 0; sub unique_stamp { return join '_', $$, time, $counter++; }

    The PID makes it unique between processes. The time makes it unique in case of PID rollover. The counter makes it unique when the same process issues two stamps inside the granularity of the timer. It also makes the granularity of the time not so important.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://483151]
Approved by Old_Gray_Bear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2018-07-21 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (446 votes). Check out past polls.

    Notices?