【发布时间】:2019-01-29 20:00:33
【问题描述】:
考虑到我在 Base 对象中有这个字段:
//these fields have their bits constantly permuted
//and have their values defined to "011233455677..."
//to integers acts like Strings
var ul = 0x011233
var ur = 0x455677
var dl = 0x998bba
var dr = 0xddcffe
考虑到我有这种方法可以将这些字段作为字符串值操作:
private fun Cubo.isSolved(): Boolean {
val solved = Base()
//function to append "leading zero" to the hex integer
fun fill(s: String) = if (s.length == 5) "0$s" else s
//all these fields are declared at the global escope
//converted ul, ur, dl, dr from solved obj to hex string
a1 = fill(Integer.toHexString(solved.ul))
b1 = fill(Integer.toHexString(solved.ur))
c1 = fill(Integer.toHexString(solved.dl))
d1 = fill(Integer.toHexString(solved.dr))
//concats the converteds ul and ur into a new one
ab1 = a1 + b1
//concats the converteds dl and dr into a new one
cd1 = c1 + d1
//do the same with fields of THIS object
a2 = fill(Integer.toHexString(this.ul))
b2 = fill(Integer.toHexString(this.ur))
c2 = fill(Integer.toHexString(this.dl))
d2 = fill(Integer.toHexString(this.dr))
ab2 = a2 + b2
cd2 = c2 + d2
//checks if concatenated fields from THIS object exists inside the
//duplicated [solved] object fields.
//This will help me to check if fields from THIS object are
//cyclic/circular permutations of the [solved] object.
return (ab1 + ab1).contains(ab2) && (cd1 + cd1).contains(cd2)
}
一旦字段为整数,我的目标是知道如何用按位运算替换该运算?
我正在尝试这个,因为我的应用程序非常慢,并且这种方法在循环调用一千次后会降低其性能,然后意识到提高我的应用程序性能的一种方法可能是使用按位运算。
为了简化这个方法的思想,它只是用来验证字段 THIS 对象对应于“SOLVED”对象的字段,但这样做是考虑到测试对象的字段可能是循环排列。
【问题讨论】:
-
您是否进行了分析以知道这需要很长时间?
-
第一行的
val solved = Base()是什么原因?换句话说,solved(及其字段)对于给定的运行实际上是一个常数吗? -
@EpicPandaForce,当我在 Android 模拟器中运行它时,该方法的使用会增加 20~30 秒到迭代的最终时间。可以说我在 600000 次循环中运行此方法。
-
@SergGr
solvedvar 只是为了获取原始值以与当前修改后的值进行比较。
标签: java string algorithm kotlin bitwise-operators