Recent

Author Topic: [SOLVED] How to short time of checking array of char/int?  (Read 7267 times)

d2010

  • Sr. Member
  • ****
  • Posts: 264
[SOLVED] How to short time of checking array of char/int?
« on: December 28, 2024, 09:30:57 am »
hello..
I found the old-function shc_k3d.
Okai is with shortstring , but you can replace "shortstring" with string, or you can
replace "shortstring" with S1big,S2:array of integer.
Code: [Select]
Function shc_k3d(S1big,S2: shortstring):integer;
Var gap,mid,nth:integer;
Label VbCanExi_click;
Begin result:=0000;if (Length(S1big)<Length(S2)or(Length(s2)<1) then exit;
      result:=RTGOOD;mid:=Length(s2) shr 02;gap:=000;
      if (mid>1)and(pdouble(@S1[mid-2])^ = pdouble(@S2[mid-2])^) then else goto VbCanExi_click;
      gap:=001;for nth:=01 to length(s1) do
            if (S1[nth]=S2[nth]) Then else Begin gap:=-nth;break;End;
      Result:=Length(s1);
exit;
VbCanExi_click:   
2024, anul canonizarii ereziei - 2025, Dumnezeu cu mila - Doamne ajuta si mantuire
 
« Last Edit: December 31, 2024, 03:23:28 am by d2010 »

Fibonacci

  • Hero Member
  • *****
  • Posts: 875
  • FPC Unleashed FTW
Re: How to short time of checking array of char/int?
« Reply #1 on: December 28, 2024, 10:22:46 am »
This code is so incorrect that Im a bit confused. I fixed the errors, so many of them. Not only the syntax, there were even non-existent variables used.

Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. type
  4.   PLHANDLE = ^PtrInt;
  5. const
  6.   RTGOOD = 0;
  7.  
  8. function shc_k3d(S1, S2: shortstring): integer;
  9. var
  10.   gap, mid, nth: integer;
  11. label VbCanExi_click;
  12. begin
  13.   result := 0;
  14.  
  15.   if (Length(S1) < Length(S2)) or (Length(s2) < 1) then
  16.     exit;
  17.   result := RTGOOD;
  18.   mid := Length(s2) shr 01;
  19.   if (mid > 4) and (PLHANDLE(@S1[mid - 2])^ = PLHANDLE(@S2[mid - 2])^) then
  20.     {then what}
  21.   else
  22.     goto VbCanExi_click;
  23.  
  24.   gap := 001;
  25.   for nth := 01 to length(S1) do
  26.     if (S1[nth] = S2[nth]) then
  27.       {then what}
  28.     else begin
  29.       gap := -nth;
  30.       break;
  31.     end;
  32.  
  33.   Result := Length(s1);
  34.  
  35.   VbCanExi_click:
  36.  
  37.   if (gap < 000) then begin
  38.     gap := abs(gap);
  39.  
  40.     for nth := gap to length(S1) do
  41.       if (S1[nth] in ['A'..'Z']) then
  42.         inc(S1[nth], 32);
  43.     for nth := gap to length(s2) do
  44.       if (s2[nth] in ['A'..'Z']) then
  45.         inc(s2[nth], 32);
  46.  
  47.     result := ord(not (S1 = s2));
  48.   end;
  49. end;//off-shc_k3d("return 1.is equal")
  50.  
  51. var
  52.   kbasename: string;
  53.  
  54. begin
  55.   kbasename := 'devcinfo.exe';
  56.  
  57.   writeln('    explorer.exe  ', shc_k3d('explorer.exe', kbasename));                 // = 0
  58.   writeln('    iexplorer.exe ', shc_k3d('iexplorer.exe', kbasename));                // = 0
  59.   writeln('    devcinfo.exe  ', shc_k3d('devcinfo.exe', kbasename));                 // = 12
  60.   writeln('    devcinfo.exe  ', shc_k3d('devcinfo.exe ^&$*Q#^$*Q@$^', kbasename));   // = 1
  61.   writeln('(x) devcinfo.exe  ', 'devcinfo.exe'.StartsWith(kbasename));               // = TRUE
  62.   writeln('(x) devcinfo.exe  ', 'devcinfo.exe ^&$*Q#^$*Q@$^'.StartsWith(kbasename)); // = TRUE
  63.   writeln('    cecolord.exe  ', shc_k3d('cecolord.exe', kbasename));                 // = 0
  64.  
  65.   readln;
  66. end.

Now tell me, where did you get this code? Did you compile it? Where is it used? What is its origin? What is the meaning of life?
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

cdbc

  • Hero Member
  • *****
  • Posts: 2725
    • http://www.cdbc.dk
Re: How to short time of checking array of char/int?
« Reply #2 on: December 28, 2024, 10:40:55 am »
Hi
Quote
Where is it used? What is its origin?
Even the mighty goooooooooooooooooooooooogle doesn't know.... %)
so, yeah... Spill, where did you get it?!?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: How to short time of checking array of char/int?
« Reply #3 on: December 28, 2024, 11:40:53 am »
I really like the "mid := length(s2) shr 2" part.
Name a variable mid and then put not the mid (half) of the lenght in it, but divide the length by 4 ...

Bart

d2010

  • Sr. Member
  • ****
  • Posts: 264
Re: How to short time of checking array of char/int?
« Reply #4 on: December 28, 2024, 11:55:43 am »
This code is so incorrect that Im a bit confused. I fixed the errors, so many of them. Not only the syntax, there were even non-existent variables used.
Now tell me, where did you get this code? Did you compile it? Where is it used? What is its origin? What is the meaning of life?

Thank/s for your solution,
In this mysource,
 my1target="how to speedup many compare of strings?"
 my2target="how to calculate "CrcInternal of each string", for fast compare".
 Tips=you can replace the mid=CrcInternal.
 If you calculate fastmode, the CrcInternal, then you got many ways to speed-up.
 I attach here the slow-speed CrcInternal inside .zip.
 ::)
PLEASE, you can calculate fast CrcInternal, not solution attached, he=very good, but is slow. I need 50000*(compare 2 strings only equal/s).
he=Str_Calc32CRC
« Last Edit: December 28, 2024, 12:02:30 pm by d2010 »

jamie

  • Hero Member
  • *****
  • Posts: 7660
Re: How to short time of checking array of char/int?
« Reply #5 on: December 28, 2024, 10:54:33 pm »
I don't want to start any un-friendly discussions about the code you posted, much of it could be caused by the way posted it thereby losing some characters.

 None the less, that code is a total mess! :(

 You need to provide an objected of values being passed and expected values being returned if you are to get any real help.

 Jamie
The only true wisdom is knowing you know nothing

silvercoder70

  • Full Member
  • ***
  • Posts: 201
    • Tim Coates
Re: How to short time of checking array of char/int?
« Reply #6 on: December 29, 2024, 11:29:03 am »
What is RTGOOD?

I'm trying to make sense of this on phone and failing badly. 

Perhaps if you can tell me me more about the code and what it does...

Because the code as posted will not compile.
🔥 Pascal Isn’t Dead -> See What It Can Do: @silvercoder70 on YouTube

d2010

  • Sr. Member
  • ****
  • Posts: 264
Re: How to short time of checking array of char/int?
« Reply #7 on: December 29, 2024, 03:32:48 pm »
Why tik is 16 and tok is 47?
Big value  is worst. Small value is better.
This trick work only all strings are lowercase.
Can you update this function?

Code: [Select]
uses SysUtils;
 
type
  PLHANDLE = ^PtrInt;
const
  RTGOOD = 5200;
  RTCAN =-5103;
  RTFAIL =-5104;

{--}
Var  ret_CompareString:integer=00;
function CompareString(const S1, S2: string;QuickCheck_I_am_sure_both_are_lowercase:boolean): pchar;
var
  I,  Last, L1, L2, C1, C2, Ch1, Ch2: Integer;
  boo:array[00..001] of boolean;
Const mymax=sizeof(double);
Label Mode_each_by_each__verygood;

begin
  L1 := Length(S1);
  L2 := Length(S2);
  boo[01]:=false;
  boo[00]:=false;
  ret_CompareString:=L1-L2;
  if (L1 > 0) and (L2 > 0) then
  begin

    if (QuickCheck_I_am_sure_both_are_lowercase)and(L1>04)and(L2>04) then
     Begin  if (L1<L2) then C1:=L1 div 02 else C1:=L2 div 02;
            boo[000]:=(c1>01)and((C1+mymax)<=L1)and((C1+mymax)<=L2);
            if (boo[00]=false) then goto Mode_each_by_each__verygood;
(*
            if (S1[c1+0] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+0]);
            if (S1[c1+1] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+1]);
            if (S1[c1+2] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+2]);
            if (S1[c1+3] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+3]);

            if (S2[c1+0] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+0]);
            if (S2[c1+1] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+1]);
            if (S2[c1+2] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+2]);
            if (S2[c1+3] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+3]);
*)
            boo[01]:=(pdouble(@S1[C1])^<>pdouble(@S2[C1])^);
           if (boo[01]) then
            Begin ret_CompareString:=ord(s1=s2);
                   if (ret_CompareString=01) then result:='boo[01] is failcalc ,must be different'
                             else result:='boo[01] is goodcalc';
                 exit;
               End;

     end;
Mode_each_by_each__verygood:
    ret_CompareString:=ord(SameText(S1,S2));
  case ret_CompareString of
   000: result:='IsNormDif';
   001: result:='IsNormEqu';
   end;
  end;
end;
{--}
var
  kbasename: string='';

Function shc_k3d(const FixBigString: string;qq:boolean):pchar;
Begin
       result:=CompareString(FixBigString,kbasename,qq);
End;

Var StrList:array[00..04] of string=('tudor - Gheorghe-porbumelul.mp3',
'yt1s.com -Serghei Rahmaninov  Vecerniile.mp3',
'Misterul_Melhisedec_în_Biblie.mp3',
'Incaltarile Sfantului Nectarie.mp4',
'orto_dimitrieshotakovich.mp3');
  Var ii,jj,tik,tok,cnt:integer;

Begin
  writeln('This program is CompareStr Tester');
  kbasename := 'orto_dimitrieshotakovich.mp3';
  for ii:=00 to high(StrList) do StrList[ii]:=lowercase(StrList[ii]);
  tik:=GetTickCount;
  for cnt:=00 to 00 do
  for ii:=00 to high(StrList) do
    Begin kbasename :=StrList[ii];
          for jj:=00 to high(StrList) do
            write(#10,ii:3,'--',jj:3,'Res=',shc_k3d(StrList[jj],true));

    End;
  tok:=GetTickCount;
  tik:=tok-tik;

  for cnt:=00 to 00 do
  for ii:=00 to high(StrList) do
    Begin kbasename :=StrList[ii];
           for jj:=00 to high(StrList) do
              write(#10,ii:3,'--',jj:3,'Res=',sametext(kbasename,StrList[jj]));
    End;
  tok:=GetTickCount-Tok;
  writeln;
  writeln('tik=',tik,'---tok=',tok);
  readln;
end.
If you replace "  Begin ret_CompareString:=ord(s1=s2);" with
                     "  Begin ret_CompareString:=001"
--then you got full source, not a tester.
 What value is  tik ? New value of tik?


« Last Edit: December 29, 2024, 03:58:43 pm by d2010 »

jamie

  • Hero Member
  • *****
  • Posts: 7660
Re: How to short time of checking array of char/int?
« Reply #8 on: December 29, 2024, 10:02:55 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. type
  7.   PLHANDLE = ^PtrInt;
  8. const
  9.   RTGOOD = 5200;
  10.   RTCAN = -5103;
  11.   RTFAIL = -5104;
  12.  
  13.   {--}
  14. var
  15.   ret_CompareString: integer = 00;
  16.  
  17.   function CompareString(const S1, S2: string;
  18.     QuickCheck_I_am_sure_both_are_lowercase: boolean): PChar;
  19.   var
  20.     I, Last, L1, L2, C1, C2, Ch1, Ch2: integer;
  21.     boo: array[00..001] of boolean;
  22.   const
  23.     mymax = sizeof(double);
  24.   label
  25.     Mode_each_by_each__verygood;
  26.   begin
  27.     L1 := Length(S1);
  28.     L2 := Length(S2);
  29.     boo[01] := False;
  30.     boo[00] := False;
  31.     ret_CompareString := L1 - L2;
  32.     if (L1 > 0) and (L2 > 0) then
  33.     begin
  34.  
  35.       if (QuickCheck_I_am_sure_both_are_lowercase) and (L1 > 04) and (L2 > 04) then
  36.       begin
  37.         if (L1 < L2) then C1 := L1 div 02
  38.         else
  39.           C1 := L2 div 02;
  40.         boo[000] := (c1 > 01) and ((C1 + mymax) <= L1) and ((C1 + mymax) <= L2);
  41.         if (boo[00] = False) then goto Mode_each_by_each__verygood;
  42. (*
  43.             if (S1[c1+0] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+0]);
  44.             if (S1[c1+1] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+1]);
  45.             if (S1[c1+2] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+2]);
  46.             if (S1[c1+3] in ['A'..'Z']) then s1[c1+0]:=lowercase(s1[c1+3]);
  47.  
  48.             if (S2[c1+0] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+0]);
  49.             if (S2[c1+1] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+1]);
  50.             if (S2[c1+2] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+2]);
  51.             if (S2[c1+3] in ['A'..'Z']) then s2[c1+0]:=lowercase(s2[c1+3]);
  52. *)
  53.         boo[01] := (pdouble(@S1[C1])^ <> pdouble(@S2[C1])^);
  54.         if (boo[01]) then
  55.         begin
  56.           ret_CompareString := Ord(s1 = s2);
  57.           if (ret_CompareString = 01) then
  58.             Result := 'boo[01] is failcalc ,must be different'
  59.           else
  60.             Result := 'boo[01] is goodcalc';
  61.           exit;
  62.         end;
  63.  
  64.       end;
  65.       Mode_each_by_each__verygood:
  66.         ret_CompareString := Ord(SameText(S1, S2));
  67.       case ret_CompareString of
  68.         000: Result := 'IsNormDif';
  69.         001: Result := 'IsNormEqu';
  70.       end;
  71.     end;
  72.   end;
  73.   {--}
  74. var
  75.   kbasename: string = '';
  76.  
  77.   function shc_k3d(const FixBigString: string; qq: boolean): PChar;
  78.   begin
  79.     Result := CompareString(FixBigString, kbasename, qq);
  80.   end;
  81.  
  82. var
  83.   StrList: array[00..04] of string = ('tudor - Gheorghe-porbumelul.mp3',
  84.     'yt1s.com -Serghei Rahmaninov  Vecerniile.mp3', 'Misterul_Melhisedec_în_Biblie.mp3',
  85.     'Incaltarile Sfantului Nectarie.mp4', 'orto_dimitrieshotakovich.mp3');
  86. var
  87.   ii, jj, tik, tok, cnt: integer;
  88.  
  89. begin
  90.   writeln('This program is CompareStr Tester');
  91.   kbasename := 'orto_dimitrieshotakovich.mp3';
  92.   for ii := 00 to high(StrList) do StrList[ii] := lowercase(StrList[ii]);
  93.   tik := GetTickCount;
  94.   for cnt := 00 to 00 do
  95.     for ii := 00 to high(StrList) do
  96.     begin
  97.       kbasename := StrList[ii];
  98.       for jj := 00 to high(StrList) do
  99.         Write(#10, ii: 3, '--', jj: 3, 'Res=', shc_k3d(StrList[jj], True));
  100.  
  101.     end;
  102.   tok := GetTickCount;
  103.   tik := tok - tik;
  104.  
  105.   for cnt := 00 to 00 do
  106.     for ii := 00 to high(StrList) do
  107.     begin
  108.       kbasename := StrList[ii];
  109.       for jj := 00 to high(StrList) do
  110.         Write(#10, ii: 3, '--', jj: 3, 'Res=', sametext(kbasename, StrList[jj]));
  111.     end;
  112.   tok := GetTickCount - Tok;
  113.   writeln;
  114.   writeln('tik=', tik, '---tok=', tok);
  115.   readln;
  116. end.
  117.  

formatted and in code tags, isn't that much better to look at?
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 6349
Re: How to short time of checking array of char/int?
« Reply #9 on: December 29, 2024, 10:31:03 pm »
something for you to think about...

presuming you only want to compare strings of equal length, if all you want is to speed up string comparisons then simply treat the strings as arrays of qwords, dwords, words, bytes as appropriate.

essentially, compare as many qwords as there are, follow by comparing as many dwords as there are, followed by as many words, followed by a lone byte comparison if the length is odd.

to get the comparison to be "string accurate" (collating sequence accurate) once there is a mismatch you got to fall back to char comparison.

The above is simple if strings are made of single byte characters, gets more complicated if dealing with mbcs.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

d2010

  • Sr. Member
  • ****
  • Posts: 264
Re: How to short time of checking array of char/int?
« Reply #10 on: December 31, 2024, 03:20:13 am »
something for you to think about...
The above is simple if strings are made of single byte characters, gets more complicated if dealing with mbcs.
One solution of your words.. You write 100%corectly, this routine work only all
strings, will be statically strings,(eg. ResourceString).
One solution of your words, otherwise maybe exists even other solutions/ but even one is..you add after this line
 for ii := 00 to high(StrList) do StrList[ii] := lowercase(StrList[ii]);
Other Line
 for ii := 00 to high(StrList) do StrList[ii] := lowercase(StrList[ii]);
 for ii := 00 to high(StrList) do StrList[ii] := mbcs_erase(StrList[ii]);
I think the routine need too many test/s with thousands of strings,
You have in mind the routine have, already, two version/s
a)     Begin ret_CompareString:=ord(s1=s2);  --> half version ,or self-check
b)     Begin ret_CompareString:=001 ; --> normnal version of self.

Merry Chritsmas, God Bless you.
Merry Chritsmas, God Bless you.
« Last Edit: December 31, 2024, 03:27:09 am by d2010 »

 

TinyPortal © 2005-2018