0
Posted on Friday, August 25, 2017 by 醉·醉·鱼 and labeled under
修饰,是设计模式里面提到的,不影响原始的对象,给对象附上多个组件,让其能够支持多个方法。https://robots.thoughtbot.com/evaluating-alternative-decorator-implementations-in提到了几种方法,对比了一下,觉得SimpleDelegator还不错。对于简单的case,还可以直接用Forwardable。

相对于继承,修饰的好处是不需要像继承一样创建很多方法,直接就可以转到原始对象上去。



require 'delegate'

class Coffee
  def cost
    2
  end

  def origin
    "Colombia"
  end
end


module DecoratorClass
  def class
    __getobj__.class
  end
end

class Milk < SimpleDelegator
  include DecoratorClass

  def cost
    super + 0.4
  end
end

class Sugar < SimpleDelegator
  include DecoratorClass

  def cost
    super + 0.2
  end
end

p Milk.ancestors
coffee = Coffee.new
p Sugar.new(Milk.new(coffee)).cost   # 2.6
p Sugar.new(Sugar.new(coffee)).cost  # 2.4
p Milk.new(coffee).origin            # Colombia
p Sugar.new(Milk.new(coffee)).class  # Coffee
0
Posted on Thursday, August 24, 2017 by 醉·醉·鱼 and labeled under
最近在http://exercism.io/上面刷题玩,遇到字母算术题。这个应该是目前最难的一道题了。实在不知道如何解,于是在网上搜了一下。找到一个算法,其实就是穷举法。
http://www.poboke.com/study/python-solve-alphametics.html
简单换成Ruby,还可以工作。

其实内容和Python一样的,只是换成ruby而已。刚好Ruby里面也有permutation这样的方法,直接就可以用了。但是性能不是很好,虽然代码行数比较少。

题外话:说Python比Ruby简洁的,你们试试把Ruby的end抹掉以后再对比行数吧。

module Alphametics
  def self.solve(input)
    words = input.scan(/\w+/)
    uniq_characters = words.join.chars.uniq
    answer_list = []
    (0..9).to_a.permutation(uniq_characters.size).each do |e|
      next if words.any? { |word| word.tr(uniq_characters.join, e.join(''))[0] == '0' }
      if instance_eval(input.tr(uniq_characters.join, e.join('')))
        answer_list = e
        break
      end
    end
    return {} if answer_list.empty?
    Hash[Hash[uniq_characters.zip answer_list].sort]
  end
end
1
Posted on Wednesday, August 23, 2017 by 醉·醉·鱼 and labeled under


https://www.toptal.com/ruby/ruby-concurrency-and-parallelism-a-practical-primer
https://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
http://blog.ifyouseewendy.com/blog/2016/02/16/ruby-concurrency-in-theory/#concurrency-vs-paralelism

并发 V.S. 并行
concurrency v.s. parallelism
如果理解没有偏差,并行就像是4个进程各自利用一个CPU执行任务。有的进程快些,有的进程慢些,但都无所谓,至少是可以一块执行的。
并发,只是说要同时处理很多事情,无关乎并行与否。如果在单核上面,启动多个线程,CPU的调度会频繁在线程之间切换,看上去是并行,实际上不是的。

在Ruby 1.9之后,可以支持多个native thread。可是,由于GIL的存在,同一时刻只允许一个线程在执行,多线程没法充分利用多核。但是,Ruby是可以利用多核的,那就是你启动多个进程,那么就可以利用多核了。

0
Posted on Wednesday, July 05, 2017 by 醉·醉·鱼 and labeled under
最近数据库服务器很慢,跑了一下whoIsActive,发现有很多waittype是RESOURCE_SEMAPHORE,发现很是好玩,整理一下。

一个成功的查询操作会引入3个内存的使用,即查询语句的编译,查询计划的缓存以及数据的缓存。

如何查看编译需要的内存,数据缓存,以及cached plan size

    1. 通过Actual query plan是最快的方法。第一个就是cached plan size(KB), 此外还有CompileMemory(B)和MemoryGrantInfo下的GrantedMemory(KB)。
    2. 在执行计划中查看
    3. 用SQL profiler捕获show plan XML for query compile event,进而在XML查看。其实拿到的数据和第二步一样。

如何模拟RESOURCE_SEMAPHORE

    1. https://www.brentozar.com/archive/2013/08/query-plans-what-happens-when-row-estimates-get-high/ 上例举了两个query。在SSMS里面,多开几个窗口,同时跑第二个query就行了。

RESOURCE_SEMAPHORE情况下,有哪些有趣的数据

    1. sp_whoIsActive 执行sp_whoIsActive 就可以直观看到哪个session已经相应的query
    2. sys.dm_exec_query_resource_semaphores 可以看到当前的内存情况
    3. sys.dm_exec_query_memory_grants 可以看到session获取内存的情况

RESOURCE_SEMAPHORE情况下,所有query都会suspend么?

    1. 不会。如果你的query还是像Brent Ozar提到的第二个query一样需要很多的内存,那就妥妥地等着吧。如果你的query需要的内存很少,那SQL server会直接执行。

引用

https://www.brentozar.com/archive/2013/08/query-plans-what-happens-when-row-estimates-get-high/
https://blogs.msdn.microsoft.com/sqlqueryprocessing/2010/02/16/understanding-sql-server-memory-grant/
0
Posted on Friday, June 09, 2017 by 醉·醉·鱼 and labeled under
熊在微信上找我,问解数独的三种方法。我第一反应是写段代码去解,就哼哧哼哧地写了。后来才发现,她只是要方法而已,就直接丢给链接过去了。(http://www.conceptispuzzles.com/zh/index.aspx?uri=puzzle/sudoku/techniques)

但自己还是把这段代码写完了,试了一下,能够解出唯一结果,如果有多个可能就不行了(暂时把可能性列出来了而已)。
最新代码已经上传到 https://github.com/zhongxiao37/zhongxiao37.github.com/blob/master/sudo/sudo.rb

0
Posted on Monday, May 22, 2017 by 醉·醉·鱼 and labeled under ,
Python 和 C 一样,有形参和实参的概念。但是我用ruby的时候,却没有这样的感觉。我每次都会data = function(data),这样的话,我就不需要去担心到底是传递值还是传递指针。但是,就Ruby而言,传递的是对象的引用。详情可以参考:http://www.iteye.com/topic/1117575




0
Posted on Thursday, May 04, 2017 by 醉·醉·鱼 and labeled under
今天发现个新问题。发现在循环中声明变量并不会reset 变量值。

DECLARE @i INT = 0;

WHILE @i < 10
BEGIN
    DECLARE @b int

    print CONCAT('PREV:', @b)

    SET @b = @i

    PRINT CONCAT('NOW:', @b)

    SET @i = @i+1

END


执行上面的query,你会发现PREV的输出并不一致是空,而是有值的。顺手搜了一下,发现很多人都发现这个问题,但是却没有一个官方文档解释这个。立即创建一个规则去避免这坑。