haibinpro 发表于 2022-7-13 10:41:17

Req 一个关于 caculate 或 bit 操作的高效脚本

目前我正在做一些需要使用高效脚本的项目
部分脚本如下所示
      (setq _j
      (Boole 6
          (rem (/ _i 256) 256)
          (rem (/ (/ _i 256) 256) 256)
          (rem (/ (/ (/ _i 256) 256) 256) 256)
      )
      )
我正在尝试优化它,使用位操作方式
      (setq _j
      (Boole 6
          (lsh (lsh (lsh _i -8) 24) -24)
          (lsh (lsh (lsh _i -16) 16) -16)
          (lsh (lsh (lsh _i -24) 8) -8)
      )
      )
这似乎需要更多的时间来计算
完整的脚本是
(defun c:tt3()
(setq TIME0 (getvar "millisecs"))
(repeat 3
    (setq _i 1)
    (setq nl '())
    (while (
      (setq _j
      (Boole 6
          (rem (/ _i 256) 256)
          (rem (/ (/ _i 256) 256) 256)
          (rem (/ (/ (/ _i 256) 256) 256) 256)
      )
      )
      (setq nl (cons _j nl))
      (setq _i (1+ _i))
    )
)
(princ "\nrem require")
(print (/ (- (getvar "millisecs") TIME0 0.0) 1000.0))
(setq TIME0 (getvar "millisecs"))
(repeat 3
    (setq _i 1)
    (setq nl '())
    (while (
      (setq _j
      (Boole 6
          (lsh (lsh (lsh _i -8) 24) -24)
          (lsh (lsh (lsh _i -16) 16) -16)
          (lsh (lsh (lsh _i -24) 8) -8)
      )
      )
      (setq nl (cons _j nl))
      (setq _i (1+ _i))
    )
)
(princ "\nlsh require")
(print (/ (- (getvar "millisecs") TIME0 0.0) 1000.0))
(princ)
)

并且输出是
rem要求
6.296
lsh要求
22.047
是任何人都可以优化它
**** Hidden Message *****

haibinpro 发表于 2022-7-13 17:38:18

我不确定你的两种方法是否完全等价...你想这样做吗?
(defun f1 ( _i )
    (boole 6
      (lsh (lsh _i 16) -24)
      (lsh (lsh _i8) -24)
      (lsh _i -24)
    )
)
如果是这样,这里有几个替代方案:
(defun f2 ( _i )
    (boole 6
      (lsh (logand _i    65280)-8)
      (lsh (logand _i 16711680) -16)
      (lsh _i -24)
    )
)(defun f3 ( _i )
    (lsh (boole 6 (lsh _i 16) (lsh _i 8) _i) -24)
)

haibinpro 发表于 2022-7-14 11:37:02

是的,你说得对。
我尝试对除低8位之外的每个8位进行xor运算。
set _i项目是一个32位的整数。
我将25-32位、17-24位、9-16位进行异或运算,得到一个8位结果,并将其赋给_j项。
f1、f2、f3运行良好。它完成了工作,
但仍需提高效率。
在我的电脑中。
calculate _i从1到1148576。f1用了5.2秒~ 5.4秒,
f2用了4.2秒~ 5.2秒,
f3用了3.4秒~ 3.7秒,但是作为第一层后的参考方法,
它用了2.2秒~ 2.3秒。
欢迎任何知道如何有效使用脚本或其他方法来完成工作的人。Thx。
提示。65280 FF00 16711680 ff 0000
页: [1]
查看完整版本: Req 一个关于 caculate 或 bit 操作的高效脚本