Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to Generate Version 1 UUIDs
Message
From
03/10/2017 01:38:54
Walter Meester
HoogkarspelNetherlands
 
 
To
02/10/2017 09:43:59
Mike Yearwood
Toronto, Ontario, Canada
General information
Forum:
Visual FoxPro
Category:
Client/server
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
SAMBA Server
Database:
MySQL
Application:
Desktop
Miscellaneous
Thread ID:
01654423
Message ID:
01654739
Views:
54
Mike,

It is very simple as you know yourself. Keep it simple. The programmer after you will thank you for that. Relying on VFP quircks like API declarations taking precedence on function is something that even most seasoned programmers do not know, is a big no-no. If you program in teams or your code is otherwise exposed to 3rd parties, your better keep things organised and simple. And even for yourself if you look at your own code 2 years from know you might actually think "What the hell did I smoke at that time?".


As I said, if you want to show off with your cleverness in reaching maximum performance in VFP that is fine, but programming is not a contest of creating maximum performance. And in your case it creating a million GUID keys in a second or 2. Well great, but ussually you want to insert those keys into records and save those which take a Thousand times as long to progress. So performance gains of your routines are absolutely irrelevant (0.3 seconds for a million keys). Sticking your energy in creating maintainable code (for everyone including yourself) pays off a lot more than trying to optimize code that is not your performance bottleneck.

And even that aside, your code could have been even faster in just declaring the API in the calling routine or instantiating a class only loading and unloading the declaration and then calling the API directly without the IIF and EXECSCRIPT in your code.

Walter,





>I come up with something unique which even you admit is fast yet you disparage it. What rules are being violated oh great master? Please cite literature on the topic and not your interpretations thereof.
>>>Having the declares fire every time you want to generate a guid is slow.
>>>
>>>Look at the bottom of this:
>>>
>>>http://fox.wikis.com/wc.dll?Wiki~GUIDGenerationCode~Wiki
>>
>>Ooww.. that is bad design. Relying on VFP specific principles and breaking several other rules that is only known to very few is very, very bad.
>>Much better is to write a class that can be instantiated where the declaration of the API occurs only once in the declaration. Keep it simple.
>>
>>Unless you want to win a performance design contest this example should never be practiced. The following is only 15% slower. But hey, who cares about < 0.3 seconds for a million keys? Certainly not the programmer who has to maintain the code.
>>
>>
>>m.lnLoopSize = 1000000
>>m.lnStart=SECONDS()
>>
>>oGuid = CREATEOBJECT("Guid")
>>STORE REPLICATE(CHR(0),16) TO m.gcuuidcreatesequential
>>
>>FOR m.lnLoop = 1 to m.lnLoopSize
>>	m.lcJunk = oGuid.uidCreateSequential(@m.gcuuidcreatesequential)
>>ENDFOR m.lnLoop
>>
>>?SECONDS()-m.lnStart,"my guids"
>>
>>
>>DEFINE CLASS Guid AS Custom
>>
>>	FUNCTION Init
>>		DECLARE INTEGER UuidCreateSequential IN 'RPCRT4.dll'  STRING @ gcuuidcreatesequential
>>	ENDFUNC
>>	
>>	*-
>>
>>	FUNCTION uidcreatesequential(m.gcuuidcreatesequential)
>>		RETURN EVL(UuidCreateSequential(@m.gcuuidcreatesequential),"GUID Error")
>>	ENDFUNC
>>ENDDEFINE
>>
>>
>>Walter,
>>
>>>
>>>>Hi Cetin,
>>>>
>>>>Worked like a charm! Thanks Tore, Gregory and Mike for your inputs!
>>>>
>>>>Actually what Tore shared also worked. Unfortunately, I can only tag one as "the solution".
>>>>
>>>>I will be using this for my "child tables", e.g. Invoice Details. I used to put an autoinc column to make the invoice details always shown in the sequence they were entered. Using UUID 4 and without either a timestamp or autoinc column, it jumbles up the order the invoice detail rows. With this, I can do away with the additional autoinc or datetime columns because it ensures rows are created in the proper sequence.
>>>>
>>>>Long live the Fox!
>>>>
>>>>
>>>>
>>>>>
>>>>>*uniqueIDSequential.prg
>>>>>Declare Integer CoCreateGuid In 'OLE32.dll' ;
>>>>>  string @pguid
>>>>>Declare Integer StringFromGUID2 In 'OLE32.dll' ;
>>>>>  string rguid, String @lpsz, Integer cchMax
>>>>>Declare Integer UuidCreateSequential In 'RPCRT4.dll'  String @ Uuid
>>>>>
>>>>>Local pguid,rguid, lcOldError, lnResult
>>>>>pguid=Replicate(Chr(0),16)
>>>>>rguid=Replicate(Chr(0),80)
>>>>>lcOldError = On('error')
>>>>>On Error lnResult = CoCreateGuid(@pguid)
>>>>>lnResult = UuidCreateSequential(@pguid)
>>>>>On Error &lcOldError
>>>>>Return ;
>>>>>  Iif( InList(m.lnResult,0,1824) And ;
>>>>>  StringFromGUID2(pguid,@rguid,40) # 0, ;
>>>>>  StrConv(Left(rguid,76),10), "" )
>>>>>
Previous
Reply
Map
View

Click here to load this message in the networking platform