StrTran

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

Post Reply
User avatar
Horst
Posts: 327
Joined: Tue Oct 13, 2015 3:27 pm

StrTran

Post by Horst »

Hallo
In meinem Code brauche ich tonnenweise Strtran () nun habe ich mal ein 51MB in einen String geladen und den mit StrTran und Net.Replace bearbeitet.

cTextIn := myReadAllLines (WorkDir()+"test.txt")
SELF:WriteCgiLog (NTrim (Seconds()))
cTextOut := cTextIn:Replace ("Variables","Lolobeifuerzwei")
SELF:WriteCgiLog (NTrim (Seconds()))
cTextOut2 := StrTran (cTextIn ,"Variables","Lolobeifuerzwei")
SELF:WriteCgiLog (NTrim (Seconds()))

StrTran braucht die doppelte Zeit. Wie kann man nachschauen , wie StrTran umgesetzt wurde?

Ich habe dann noch das versucht und das ist gleich schnell wie Variante 1
cTextOut3 := myStrTran (@cTextIn ,"Variables","Lolobeifuerzwei")
FUNCTION myStrTran (cInput REF STRING, cSuch AS STRING, cReplace AS STRING) AS STRING
RETURN cInput:Replace(cSuch,cReplace)

Liegt es daran, dass StrTran noch nStart und nCount hat ?
Gruss
Horst
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

StrTran

Post by wriedmann »

Hallo Horst,
StrTran() ist nicht streng typisiert, das macht sicher was aus.
Ansonsten: in der X# Hilfe gibt es einen Button "Source":
https://github.com/X-Sharp/XSharpPublic ... String.prg
Eine .NET-Methode ist sicher schneller, muss aber per try-catch abgesichert werden.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Horst
Posts: 327
Joined: Tue Oct 13, 2015 3:27 pm

StrTran

Post by Horst »

Hallo Wolfgang
Wieso muss sie abgesichert werden ?
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

StrTran

Post by wriedmann »

Hallo Horst,
da gab es erst kürzlich eine Diskussion drüber.
Die meisten Methoden in .NET werfen eine Exception, wenn eine nicht erwartete Situation auftritt, z.B. ein übergebener String Null ist oder so ähnlich.
String.Replace scheint da eh recht tolerant zu sein, die Methode verträgt es laut Doku nur nicht, wenn der Quellstring entweder Null oder leer ist (StrTran macht es nichts aus, wenn der erste Parameter "" ist).
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

StrTran

Post by Chris »

Guys,

When the input string is 51 MB, I doubt that there's any noticeable speed difference because of StrTran() being untyped, you would definitely notice this difference for many calls to StrTran() with smaller strings.

I suspect that the main reason why String.Replace() is faster than StrTran() with huge strings, is that it is implemented internally in the Framework, probably in c++ or assembly, which will always be faster than a function implemented in IL, like STrTran().
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Jamal
Posts: 314
Joined: Mon Jul 03, 2017 7:02 pm

StrTran

Post by Jamal »

Hi All,

Chris is right (of course). The .NET string.replace internally calls a C++ function.

https://github.com/g15ecb/shared-source ... .cpp#L1572

Horst, while the X# StrTran() may be more flexible, however, you may create a wrapper to call the string.replace; Still it should be much faster.

I believe, the same can be said about the .NET string methods listed below (C#) which would be much faster due to underlying c++ code:

Concat(Object)
Insert(Int32, String)
Join(String, String[])
Remove(Int32, Int32)
Split(Char[])
Substring(Int32)
Trim(Char[])

Jamal
Post Reply