惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Forbes - Security
Forbes - Security
WordPress大学
WordPress大学
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
P
Privacy International News Feed
A
About on SuperTechFans
T
Tailwind CSS Blog
I
InfoQ
S
Securelist
云风的 BLOG
云风的 BLOG
罗磊的独立博客
Recent Announcements
Recent Announcements
T
The Exploit Database - CXSecurity.com
B
Blog RSS Feed
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
腾讯CDC
Cyberwarzone
Cyberwarzone
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
S
Secure Thoughts
博客园 - 司徒正美
J
Java Code Geeks
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
Help Net Security
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Project Zero
Project Zero
T
Tenable Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tor Project blog
MyScale Blog
MyScale Blog
Scott Helme
Scott Helme
小众软件
小众软件
K
Kaspersky official blog

博客园 - Wisdom-zh

【关注】“诋毁” 一下博客园的模板 混淆器【修正版】 发一个混淆器, Nuva 写的 Win32Ole 演示【控制 Word】 Win32Api 演示【控制任务栏】 通用数据浏览示例 方便的 XML 数据绑定 Swap 函数 延迟计算 语法兼容 之 运算符总结 这是什么语言的代码 Nuva 语法兼容 之 控制结构总结 设计模式 之 状态机模式 Nuva 示例代码(每日一帖)之 源代码统计 Nuva 示例代码(每日一帖)之 数据架构提取 Nuva 示例代码(每日一帖)之 ShowMessage Nuva 示例代码(每日一帖)之 异常处理 Nuva 示例代码(每日一帖)之 语法兼容 挑战 XSLT 最擅长的 XML 文档转换
短路运算
Wisdom-zh · 2006-09-21 · via 博客园 - Wisdom-zh
<..========================================================
==                                                       ==
==                Macrobject Nuva Samples                ==
==                                                       ==
==      Copyright (c) 2004-2006 Macrobject Software      ==
==                                                       ==
==                  ALL RIGHTS RESERVED                  ==
==                                                       ==
==               http://www.macrobject.com               ==
==                                                       ==
========================================================..>
<.
  function T1()
    result = true
    ?? 'T1'
  end function
  
  function T2()
    result = true
    ?? 'T2'
  end function
  
  function F1()
    result = false
    ?? 'F1'
  end function
  
  ?? T1() and T2() and F1()
  ?? '-------------------------------'
  ?? T1() and ( T2() or F1() )
  ?? '-------------------------------'
  ?? F1() or T1() and T2()
  ?? '-------------------------------'
  ?? F1() and T1() and T2()
  ?? '-------------------------------'
  ?? F1() or F1() and T1() and T2()
  ?? '-------------------------------'
  ?? not F1() or F1() and T1() and T2()
  ?? '-------------------------------'
.>


【简介】
    短路计算通常用在逻辑表达式中,如果从逻辑表达式的第一个部分(或先计算的某个部分)就能推测整个逻辑表达式的值,从而免去计算整个表达式的部分时间,因此可以明显得到性能的提升。

【看点】
    短路计算除了可以提高计算性能以外,还有一些明显的好处,看如下的代码:
   

    <.
      var node  
      if (node <> nil and node.Parent <> nil)
        ?? 'node <> nil and node.Parent <> nil'
      end if
      
      if (node = nil or node.Parent = nil)
        ?? 'node = nil or node.Parent = nil'
      end if
    .>

    
    如果在不支持逻辑表达式短路计算的编程语言中执行上面的代码,就会有可能出现异常,因为上面的变量 node 有可能为空。这样必须采用下面的代码才能避免异常:
   

    <.
      var node  
      if (node <> nil)
        if (node.Parent <> nil)
          ?? 'node <> nil and node.Parent <> nil'
        end if
      end if
      
      if (node = nil)
        ?? 'node = nil or node.Parent = nil'
      elseif (node.Parent = nil)
        ?? 'node = nil or node.Parent = nil'
      end if
    .>

    
    因此,短路计算除了能够提高计算性能,还能明显的改善程序的书写,更加简洁并且易读。

【扩展】
    本例是一个语法兼容示例,无扩展需求,以后将逐步编写更多的语法兼容示例。

【运行结果】
T1
T2
F1
False
-------------------------------
T1
T2
True
-------------------------------
F1
T1
T2
True
-------------------------------
F1
False
-------------------------------
F1
F1
False
-------------------------------
F1
True
-------------------------------