如您所知,Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE为了防止出现负值,该safeAbs方法已在我的项目中实现:
public static int safeAbs(int i) {
i = Math.abs(i);
return i < 0 ? 0 : i;
}
Run Code Online (Sandbox Code Playgroud)
我将性能与以下各项进行了比较:
public static int safeAbs(int i) {
return i == Integer.MIN_VALUE ? 0 : Math.abs(i);
}
Run Code Online (Sandbox Code Playgroud)
并且第一个比第二个慢几乎6倍(第二个性能几乎与“纯” Math.abs(int)相同)。从我的角度来看,字节码没有显着差异,但是我猜想差异存在于JIT“汇编”代码中:
“慢”版本:
0x00007f0149119720: mov %eax,0xfffffffffffec000(%rsp)
0x00007f0149119727: push %rbp
0x00007f0149119728: sub $0x20,%rsp
0x00007f014911972c: test %esi,%esi
0x00007f014911972e: jl 0x7f0149119734
0x00007f0149119730: mov %esi,%eax
0x00007f0149119732: jmp 0x7f014911973c
0x00007f0149119734: neg %esi
0x00007f0149119736: test %esi,%esi
0x00007f0149119738: jl 0x7f0149119748
0x00007f014911973a: mov %esi,%eax
0x00007f014911973c: add $0x20,%rsp …Run Code Online (Sandbox Code Playgroud) 我没有(也不会)拥有管理员帐户。我想从 java 更改 Active Directory 中自己(用户)的密码。我怎样才能做到这一点?
使用来自网络的代码:
private void changePass() throws Exception {
String oldpass = this.encodePassword("oldpass!");
String newpass = this.encodePassword("newpass!");
Attribute oldattr = new BasicAttribute("unicodePwd", oldpass);
Attribute newattr = new BasicAttribute("unicodePwd", newpass);
ModificationItem olditem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, oldattr);
ModificationItem newitem = new ModificationItem(DirContext.ADD_ATTRIBUTE, newattr);
ModificationItem repitem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, newattr);
ModificationItem[] mods = new ModificationItem[2];
mods[0] = olditem;
mods[1] = newitem;
// ldapTemplate.modifyAttributes("cn=administrator,cn=Users", mods);
ldapTemplate.modifyAttributes("cn=smith,cn=Users", new ModificationItem[] { repitem });
}
Run Code Online (Sandbox Code Playgroud)
这是上下文源
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://ldapserver:389"/>
<property …Run Code Online (Sandbox Code Playgroud)