Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Remove excessive spaces in string
Message
De
24/07/2009 20:14:42
 
 
À
24/07/2009 19:49:05
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01414370
Message ID:
01414415
Vues:
53
When introducing the support for multiple passes, I neglected to reset the test string value to the original value for the Reduce_Successive and Reduce_Loop functions, making them look a lot better than they actually are.

Fixed code:
CLEAR

SET TALK OFF
SET DECIMALS TO 3

SET LIBRARY TO ( HOME( ) + "FoxTools.FLL" ) ADDITIVE

LOCAL ;
	lnPasses

lnPasses = 100

=RunTest( "2", lnPasses ) && 2^2 = 4 spaces
=RunTest( "4", lnPasses ) && 2^4 = 16 spaces
=RunTest( "8", lnPasses ) && 2^8 = 256 spaces
=RunTest( "16", lnPasses ) && 2^16 = 65,536 spaces
=RunTest( "20", lnPasses ) && 2^20 spaces

RETURN
*************************************************************
FUNCTION RunTest

LPARAMETERS ;
	tcSpacesExponent ;
	, tnPasses

?"Spaces: 2^" + tcSpacesExponent ;
	+ "  Passes: " + LTRIM( STR( tnPasses ) ) ;
	+ "  Milliseconds: " + LTRIM( STR( Reduce_FoxTools( SPACE( EVALUATE( "2^" + tcSpacesExponent ) ), tnPasses ) ) ) ;
	+ "  (REDUCE in FoxTools.FLL)"

?"Spaces: 2^" + tcSpacesExponent ;
	+ "  Passes: " + LTRIM( STR( tnPasses ) ) ;
	+ "  Milliseconds: " + LTRIM( STR( Reduce_Successive( SPACE( EVALUATE( "2^" + tcSpacesExponent ) ), tnPasses ) ) ) ;
	+ "  (Reduce_Successive)"

?"Spaces: 2^" + tcSpacesExponent ;
	+ "  Passes: " + LTRIM( STR( tnPasses ) ) ;
	+ "  Milliseconds: " + LTRIM( STR( Reduce_Loop( SPACE( EVALUATE( "2^" + tcSpacesExponent ) ), tnPasses ) ) ) ;
	+ "  (Reduce_Loop)"

RETURN
*************************************************************
FUNCTION Reduce_FoxTools

LPARAMETERS ;
	tcString ;
	, tnPasses

LOCAL ;
	lnSeconds ;
	, lnIx ;
	, lcString

lnSeconds = SECONDS( )

FOR lnIx = 1 TO tnPasses STEP 1
	lcString = tcString
	
	=REDUCE( lcString )

ENDFOR

RETURN ( SECONDS( ) - lnSeconds ) * 1000

ENDFUNC
*************************************************************
FUNCTION Reduce_Successive

LPARAMETERS ;
	tcString ;
	, tnPasses

LOCAL ;
	lnSeconds ;
	, lnIx ;
	, lcString

lnSeconds = SECONDS( )

FOR lnIx = 1 TO tnPasses STEP 1
	lcString = tcString
	
	* Code below handles spaces up to 2^20 long (fails on 2^20 + 1)

	lcString = STRTRAN( lcString, [                ], [ ] )
	lcString = STRTRAN( lcString, [                ], [ ] )
	lcString = STRTRAN( lcString, [        ], [ ] )
	lcString = STRTRAN( lcString, [        ], [ ] )
	lcString = STRTRAN( lcString, [    ], [ ] )
	lcString = STRTRAN( lcString, [    ], [ ] )
	lcString = STRTRAN( lcString, [  ], [ ] )
	lcString = STRTRAN( lcString, [  ], [ ] )

ENDFOR

RETURN ( SECONDS( ) - lnSeconds ) * 1000

ENDFUNC
*************************************************************
FUNCTION Reduce_Loop

LPARAMETERS ;
	tcString ;
	, tnPasses

LOCAL ;
	lnSeconds ;
	, lnIx ;
	, lcString

lnSeconds = SECONDS( )

FOR lnIx = 1 TO tnPasses STEP 1
	lcString = tcString
	
	DO WHILE [  ] $ lcString
		lcString = STRTRAN( lcString, [  ], [ ] )

	ENDDO
	
ENDFOR

RETURN ( SECONDS( ) - lnSeconds ) * 1000

ENDFUNC
*************************************************************
The only thing I'm not completely happy about is that the times are also measuring how long it takes to reassign lcString = tcString for the desired number of passes. However, this overhead is exactly the same for all 3 methods.

New results:
Spaces: 2^2  Passes: 100  Milliseconds: 2  (REDUCE in FoxTools.FLL)  
Spaces: 2^2  Passes: 100  Milliseconds: 1  (Reduce_Successive) 
Spaces: 2^2  Passes: 100  Milliseconds: 1  (Reduce_Loop) 
Spaces: 2^4  Passes: 100  Milliseconds: 2  (REDUCE in FoxTools.FLL)  
Spaces: 2^4  Passes: 100  Milliseconds: 1  (Reduce_Successive) 
Spaces: 2^4  Passes: 100  Milliseconds: 1  (Reduce_Loop) 
Spaces: 2^8  Passes: 100  Milliseconds: 4  (REDUCE in FoxTools.FLL)  
Spaces: 2^8  Passes: 100  Milliseconds: 2  (Reduce_Successive) 
Spaces: 2^8  Passes: 100  Milliseconds: 9  (Reduce_Loop) 
Spaces: 2^16  Passes: 100  Milliseconds: 469  (REDUCE in FoxTools.FLL)   
Spaces: 2^16  Passes: 100  Milliseconds: 132  (Reduce_Successive)  
Spaces: 2^16  Passes: 100  Milliseconds: 1739  (Reduce_Loop)  
Spaces: 2^20  Passes: 100  Milliseconds: 9906  (REDUCE in FoxTools.FLL)   
Spaces: 2^20  Passes: 100  Milliseconds: 4611  (Reduce_Successive)  
Spaces: 2^20  Passes: 100  Milliseconds: 32510  (Reduce_Loop)
These results are for only 100 passes, rather than 1,000. They are all basically identical for small numbers of spaces. However, when you get into larger numbers of spaces, my technique is fastest, followed by REDUCE in FoxTools, then Chuck's looping technique.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform