Author Topic: Req a highly efficient script about caculate or bit manipulation  (Read 1180 times)

0 Members and 1 Guest are viewing this topic.

haibinpro

  • Newt
  • Posts: 52
currently I working on some project witch need to use highly efficient script to do the work.
some of the script is as below
Code - Auto/Visual Lisp: [Select]
  1.       (setq _j
  2.         (Boole 6
  3.           (rem (/ _i 256) 256)
  4.           (rem (/ (/ _i 256) 256) 256)
  5.           (rem (/ (/ (/ _i 256) 256) 256) 256)
  6.         )
  7.       )
  8.  
I'm try to optimize it,use bit manipulation way
Code - Auto/Visual Lisp: [Select]
  1.       (setq _j
  2.         (Boole 6
  3.           (lsh (lsh (lsh _i -8) 24) -24)
  4.           (lsh (lsh (lsh _i -16) 16) -16)
  5.           (lsh (lsh (lsh _i -24) 8) -8)
  6.         )
  7.       )
  8.  
It seem need more time to caculate for it.

the complete script is
Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt3()
  2.   (setq TIME0 (getvar "millisecs"))
  3.   (repeat 3
  4.     (setq _i 1)
  5.     (setq nl '())
  6.     (while (< _i 1148576)
  7.       (setq _j
  8.         (Boole 6
  9.           (rem (/ _i 256) 256)
  10.           (rem (/ (/ _i 256) 256) 256)
  11.           (rem (/ (/ (/ _i 256) 256) 256) 256)
  12.         )
  13.       )
  14.       (setq nl (cons _j nl))
  15.       (setq _i (1+ _i))
  16.     )
  17.   )
  18.   (princ "\nrem require")
  19.   (print (/ (- (getvar "millisecs") TIME0 0.0) 1000.0))
  20.   (setq TIME0 (getvar "millisecs"))
  21.   (repeat 3
  22.     (setq _i 1)
  23.     (setq nl '())
  24.     (while (< _i 1148576)
  25.       (setq _j
  26.         (Boole 6
  27.           (lsh (lsh (lsh _i -8) 24) -24)
  28.           (lsh (lsh (lsh _i -16) 16) -16)
  29.           (lsh (lsh (lsh _i -24) 8) -8)
  30.         )
  31.       )
  32.       (setq nl (cons _j nl))
  33.       (setq _i (1+ _i))
  34.     )
  35.   )
  36.   (princ "\nlsh require")
  37.   (print (/ (- (getvar "millisecs") TIME0 0.0) 1000.0))
  38.   (princ)
  39. )
  40.  

and the ouput is

rem require
6.296
lsh require
22.047

Is anybody can  optimize it

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Req a highly efficient script about caculate or bit manipulation
« Reply #1 on: July 13, 2022, 05:38:18 PM »
I'm not sure that your two methods are entirely equivalent... are you looking to do this?
Code - Auto/Visual Lisp: [Select]
  1. (defun f1 ( _i )
  2.     (boole 6
  3.         (lsh (lsh _i 16) -24)
  4.         (lsh (lsh _i  8) -24)
  5.         (lsh _i -24)
  6.     )
  7. )

If so, here are a couple of alternatives:
Code - Auto/Visual Lisp: [Select]
  1. (defun f2 ( _i )
  2.     (boole 6
  3.         (lsh (logand _i    65280)  -8)
  4.         (lsh (logand _i 16711680) -16)
  5.         (lsh _i -24)
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. (defun f3 ( _i )
  2.     (lsh (boole 6 (lsh _i 16) (lsh _i 8) _i) -24)
  3. )
« Last Edit: July 13, 2022, 05:51:29 PM by Lee Mac »

haibinpro

  • Newt
  • Posts: 52
Re: Req a highly efficient script about caculate or bit manipulation
« Reply #2 on: July 14, 2022, 11:37:02 AM »
Yes.you are right.
I try to xor every 8 bit except the low 8 bit.
set _i item is a 32bit int.
I get the 25-32bit,17-24bit,9-16bit to xor to get a 8bit result and assign it to _j item.
the f1,f2,f3 runs well.It did the work,
but still need to be more efficient.
In my computer.
calculate _i from 1 to 1148576.
f1 used 5.2s~5.4s,
f2 used 4.2~5.2s,
f3 used 3.4~3.7s
but the ref rem way as post the 1st floor,
it use 2.2s~2.3s

In my first sense,I suppose bit cal is more efficient.but as 1st floor test,It take more time to cal.
anybody known how to efficient the script or the other method to do the job is welcome.
Thx.

Tips.
65280   FF00
16711680   FF0000

« Last Edit: July 14, 2022, 11:46:36 AM by haibinpro »