NOL

Share your advanced PureBasic knowledge/code with the community.
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: NOL

Post by pcfreak »

Here is my line count function...

Code: Select all

DataSection
  !CountLinesPcf_eoli:
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  !db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
EndDataSection


Procedure.l CountLinesPcf(filename.s)
 Define fid.i, lcCR.i, lcLF.i, *mem, size.i
 fid = ReadFile(#PB_Any, filename)
 lcCR = 0
 lcLF = 0
 If IsFile(fid)
   size = Lof(fid)
   *mem = AllocateMemory(size)
   size = ReadData(fid, *mem, size)
   CompilerSelect #PB_Compiler_Processor
   CompilerCase #PB_Processor_x86
    ;eax, ecx and edx are ok, save rest
    Define.l old_esi
    !MOV dword [p.v_old_esi], esi
    !MOV esi, dword [p.p_mem]
    !MOV ecx, dword [p.v_size]
    !XOR eax, dword eax
    !CountLinesPcf_loop:
     !MOV al, byte [esi]
     !CMP byte [eax+CountLinesPcf_eoli], byte 1
     !JNE CountLinesPcf_noLFCR
      !CMP eax, dword 13 ;CR
      !JNE CountLinesPcf_noCR
       !INC dword [p.v_lcCR]
       !JMP CountLinesPcf_noLF
      !CountLinesPcf_noCR:
       !INC dword [p.v_lcLF]
      !CountLinesPcf_noLF:
     !CountLinesPcf_noLFCR:
     !INC dword esi
     !DEC dword ecx
    !JNZ CountLinesPcf_loop
    !MOV esi, dword [p.v_old_esi]
   CompilerCase #PB_Processor_x64
    ;eax, ecx and edx are ok, save rest
    Define.q old_esi
    !MOV qword [p.v_old_esi], rsi
    !MOV rsi, qword [p.p_mem]
    !MOV rcx, qword [p.v_size]
    !XOR rax, qword rax
    !MOV rdx, qword CountLinesPcf_eoli
    !CountLinesPcf_loop:
     !MOV al, byte [rsi]
     !CMP byte [rax+rdx], byte 1
     !JNE CountLinesPcf_noLFCR
      !CMP eax, dword 13 ;CR
      !JNE CountLinesPcf_noCR
       !INC qword [p.v_lcCR]
       !JMP CountLinesPcf_noLF
      !CountLinesPcf_noCR:
       !INC qword [p.v_lcLF]
      !CountLinesPcf_noLF:
     !CountLinesPcf_noLFCR:
     !INC qword rsi
     !DEC qword rcx
    !JNZ CountLinesPcf_loop
    !MOV rsi, qword [p.v_old_esi]
   CompilerDefault
    CompilerError "unsupported CPU architecture"
   CompilerEndSelect
   FreeMemory(*mem)
  CloseFile(fid)
 EndIf
 If lcLF > 0
  ProcedureReturn lcLF
 EndIf
 ProcedureReturn lcCR
EndProcedure
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: NOL

Post by IdeasVacuum »

...Now that is fast pcfreak! :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: NOL

Post by blueb »

Just tried it on a text file that had a million lines (1,000,000) of unsorted text
and it returned: 47msecs Wow!


-blueb
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: NOL

Post by Seymour Clufley »

Thanks for all the code, people. Who would have thought there'd be so many different ways of doing the same thing?!

I'm going to try them all out now!
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply