`
langzhe
  • 浏览: 288059 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

++操作与lists:concat操作比较 (lists:concat运算结果)

 
阅读更多

测试环境

Linux jason-lxw 3.2.0-39-generic #62-Ubuntu SMP Thu Feb 28 00:28:53 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

4G内存

Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.5  (abort with ^G)

1> 

测试数据:

add()->

    III="langxw",

    MMM= 10,

    OFFFFF = 100,

    "display uuuuuuuuu, ffstate ,"      ++                

                        " uuusername, uubio, "++          

                        " uuavatar_url," ++               

                        " uusex todo langgfriendship f , langgusers u where fffolllleriii=uuuuuuuuu " ++                                                                                                                                                                                     

                        " aaa | fffollllediii = '", III, "')" ++

                        " aaa Lype='1'" ++                

                        " rrrr by ffctttte_datt ssss lllll " ++ integer_to_list(MMM) ++" FFeeee " ++ integer_to_list(OFFFFF) ++";".                                                                                                                                                          

 

 

concat()->

    III="langxw",

    MMM= 10,

    OFFFFF = 100,

    Sql = lists:concat(["display uuuuuuuuu, ffstate ,"     

                        " uuusername, uubio, ",           

                        " uuavatar_url,",                                                                                                                                                                                                                                                     

                              

                        " uusex todo langgfriendship f , langgusers u where fffolllleriii=uuuuuuuuu ",                                                                                                                                                                                       

                        " aaa | fffollllediii = '", III, "')",

                        " aaa Type='1'",                  

                        " rrrr by ffctttte_datt ssss lllll ", MMM," FFeeee ", OFFFFF,";"                                                                                                                                                                                                     

                        ]).

 

lists:concat操作结果

49> test_lists_concat:start(1,10000).  :参数:第一个参数是单进程,第二个参数是  add/concat执行次数

===============================================================

process #1 send 10000 request spent 62 ms

total spent=62 ms,avg spent=62 ms

=====================================================================

true

50> test_lists_concat:start(1,100000).

===============================================================

process #1 send 100000 request spent 554 ms

total spent=554 ms,avg spent=554 ms

=====================================================================

true

51> test_lists_concat:start(1,100 0000).

===============================================================

process #1 send 1000000 request spent 5699 ms

total spent=5699 ms,avg spent=5699 ms

=====================================================================

true

 

++操作结果

54> test_lists_concat:start(1,10000). 

===============================================================

process #1 send 10000 request spent 2 ms

total spent=2 ms,avg spent=2 ms

=====================================================================

true

55> test_lists_concat:start(1,100000).

===============================================================

process #1 send 100000 request spent 27 ms

total spent=27 ms,avg spent=27 ms

=====================================================================

true

56> test_lists_concat:start(1,100 0000).

===============================================================

process #1 send 1000000 request spent 206 ms

total spent=206 ms,avg spent=206 ms

=====================================================================

true

57> test_lists_concat:start(1,10000000).

===============================================================

process #1 send 10000000 request spent 5257 ms

total spent=5257 ms,avg spent=5257 ms

=====================================================================

 

结论:(仅代表当前测试总结学习)乍看lists:concat源码,以为lists:concat执行效率会比++操作快,但实际测试结果,lists:concat远不如++操作。这可能因为lists:concat里面的递归(非尾递归)造成的。从上面测试数据可以看出lists:concat 比++操作要慢几十倍。

 

 

 

 

 

-module(test_lists_concat).

-export([test/2]).

       


-export([start/2,dotest/3]).  
  
start(Processes,Loops)->  
    ets:new(ib,[named_table,set]),     
    [spawn(?MODULE,dotest,[self(),X,Loops])||X<-lists:seq(1,Processes)],  
    recv(Processes,Loops),  
    ets:delete(ib).  
  
recv(Processes,Loops)->  
    receive  
        {fin,ProcessNum,Elapse}->          
            ets:insert(ib,{ProcessNum,Elapse}),  
            case ets:info(ib,size) of          
                Processes->  
                    %%got all the resp                 
                    print_result(Processes,Loops);     
                _->  
                    recv(Processes,Loops)              
            end;  
        Err->  
            io:format("recv unknowen msg~n"),  
            recv(Processes,Loops)              
    end.  
  
print_result(Processes,Loops)->  
    io:format("===============================================================~n"),  
    TotalElapse=  
    lists:foldr(  
     fun({ProcessNum,Elapse},Acc)->     
        io:format("process #~p send ~p request spent ~p ms~n",[ProcessNum,Loops,Elapse]),         
        Acc + Elapse  
     end,      
     0,  
     ets:tab2list(ib)  
    ),  
    AvgElapse=TotalElapse div Processes,  
    io:format("total spent=~p ms,avg spent=~p ms~n",[TotalElapse,AvgElapse]),  
    io:format("=====================================================================~n").  
      
dotest(From,ProcessNum,Loops)->  
    Start=timestamp_in_millinsec(),    
    [lists_concat()||X<-lists:seq(1,Loops)],  
    Elapse=timestamp_in_millinsec()-Start,  
    From!{fin,ProcessNum,Elapse}.      
      
timestamp_in_millinsec()->                                                                                                                                                                                                                                                                                                  
    {MegaSec,Sec,MicoSec}=erlang:now(),  
    MegaSec * 1000000000+Sec*1000+MicoSec div 1000.  

lists_concat() ->
    %%lists:concat(["langxw", "langxw", "langxw"]),
    %%""++"langxw"++"langxw",
    %%concat(),
    add(),
    ok.

add()->
    III="langxw",
    MMM= 10,
    OFFFFF = 100,
    "display uuuuuuuuu, ffstate ,"      ++
                        " uuusername, uubio, "++
                        " uuavatar_url," ++
                        " uusex todo langgfriendship f , langgusers u where fffolllleriii=uuuuuuuuu " ++
                        " aaa | fffollllediii = '", III, "')" ++
                        " aaa Lype='1'" ++
                        " rrrr by ffctttte_datt ssss lllll " ++ integer_to_list(MMM) ++" FFeeee " ++ integer_to_list(OFFFFF) ++";".


concat()->
    III="langxw",
    MMM= 10,
    OFFFFF = 100,
    Sql = lists:concat(["display uuuuuuuuu, ffstate ,"
                        " uuusername, uubio, ",
                        " uuavatar_url,",
                        " uusex todo langgfriendship f , langgusers u where fffolllleriii=uuuuuuuuu ",
                        " aaa | fffollllediii = '", III, "')",
                        " aaa Type='1'",
                        " rrrr by ffctttte_datt ssss lllll ", MMM," FFeeee ", OFFFFF,";"
                        ]).


test(Pro, Num)->
o.

 

 

补充:

51> test_lists_concat:start(1,100 0000).

===============================================================

process #1 send 1000000 request spent 5699 ms

total spent=5699 ms,avg spent=5699 ms

 

56> test_lists_concat:start(1,100 0000).

===============================================================

process #1 send 1000000 request spent 206 ms

@石头哥哥 这两次结果比较 ++ 比lists:concat快了大约27倍。

 

@erlang-菜鸟

个人理解(没测试)假设有两个list分别是A和B

1、如果A的长度远远大约B的长度时。A++B操作 会比 B++A慢

 

2、如果1成立,同样假设有A1,A2,A3,A4...An

   如果A1长度>A2长短>A3长度>A4长度>...>An长度,

   A1++A2++A3++A4++...++An 会比

   {A4++..++An :记为R

    A3++R :记为R

    A2++R :记为R

    A1++R 

    }慢。

基于这样的理解,简单的误认为lists:concat和++的快慢显示是错误的。还有就是lists:concat里面调用了非尾递归。感兴趣的同学继续测试一下。

找时间测试研究一下++操作顺序

0
2
分享到:
评论

相关推荐

    Practical Programming in Tcl and Tk

    - **Constructing Lists: list, lappend, and concat**:讲解如何构建列表。 - **Getting List Elements: llength, lindex, and lrange**:介绍如何访问列表元素。 - **Modifying Lists: linsert and lreplace**:...

    Learning_Perl_Student_Workbook(2nd,2012.01)

    - 分组与引用:如何使用括号()进行分组并引用匹配结果。 - 模式修饰符:i、m、s等修饰符的作用。 - 匹配模式:使用=~操作符进行匹配。 ##### 9. **Processing Text with Regular Expressions** - **章节概述**:...

    微信小程序实现下滑到底部自动翻页功能

    lists: this.data.lists.concat(new_lists), page: page, }); }, onLoad: function (options) { let lists = []; // 模拟请求数据 for (let i = this.data.page; i &lt;= this.data.page + 9; i++) { lists....

    Python自动化办公+python实现合并多个excel中同名的sheet(没有同名的也可以)

    这个功能对于会计或者数据分析工作者来说非常实用,能大大提升工作效率,减少手动操作的繁琐和出错概率。 首先,我们需要导入必要的库,包括pandas和os。pandas库是Python中处理数据的主要工具,它提供了强大的数据...

    fun-with-lists

    标题“fun-with-lists”可能指的是一个与列表操作相关的JavaScript项目。这个项目旨在探索和实践JavaScript中的数组(在JavaScript中,我们通常将数组称为列表)的各种功能和操作。JavaScript是一种广泛用于网页和...

    Java Stream 流实现合并操作示例

    如果我们需要合并多个流,可以使用 concat 方法进行“套娃操作”,例如: ```java Stream&lt;Integer&gt; stream = Stream.of(1, 2, 3); Stream&lt;Integer&gt; another = Stream.of(4, 5, 6); Stream&lt;Integer&gt; more = Stream....

    C#语法说明

    17. **字符串操作**:C#中的字符串是不可变的,提供了丰富的字符串处理方法,如Concat、Substring、Replace等。 18. **枚举(Enums)**:枚举是一种强类型的数据类型,用于定义一组命名的整数值,便于代码的可读性...

    47-Arrays_and_Linked_Lists

    此外,还有`slice()`、`splice()`、`concat()`等用于操作数组元素的方法。 链表,相比之下,不连续存储元素,每个节点包含数据和指向下一个节点的引用。在JavaScript中,链表需要手动实现,因为语言本身并不直接...

    Java 后端面试题附答案

    - Redis支持多种数据类型,包括字符串(Strings)、哈希(Hashes)、列表(Lists)、集合(Sets)和有序集合(ZSets)等。 #### 25. Redis底层数据结构 - **知识点**: - Redis使用的底层数据结构。 - **解释**: - Redis...

    Vue实现拖放排序功能的实例代码

    4. JavaScript数组操作:包括concat、splice等方法,用于在拖放操作过程中对数组进行修改和排序。 具体到代码实现,我们可以从以下几个部分来详细解释知识点: ### HTML结构 在HTML中,使用`&lt;ul&gt;`元素作为列表...

    Python自动化办公+实现合并多个excel中同名的sheet

    ### Python自动化办公:实现合并多个Excel中同名的Sheet 在日常工作中,我们经常会遇到需要处理大量...通过对上述代码的理解与实践,你可以轻松地将其应用到自己的项目中,解决类似的问题。希望这篇文章对你有所帮助!

    elisp:学习Elisp

    **Elisp:深入理解与应用** Elisp,全称Emacs Lisp,是Emacs编辑器中的脚本语言,用于扩展和定制编辑器的功能。作为一款强大的文本编辑器,Emacs的强大之处在于其高度可定制性,而这正是Elisp的功劳。在本文中,...

    erl_pfds:『Purely Functional Data Structures』中描述的数据结构的 Erlang 实现

    例如,二叉搜索树(BST)可以通过比较节点值来进行排序操作。理解如何在保持纯函数式的同时实现平衡策略,如AVL树或2-3树,对于优化搜索性能至关重要。 3. **队列(Queues)**:Okasaki提出了“deque”(双端队列)...

    bochs user manual 单html文件+pdf 英文原版

    bochs的用户操作手册,英文原版。 该压缩包包含一个pdf版本的用户手册和一个单html版本的用户手册。 \ + Bochs User Manual.pdf + usermanual \+Bochs User Manual.html +images 该文件由bochs官方文档制作而成...

    Writing GNU Emacs Extensions

    例如,如果两个命令都需要修改同一个缓冲区的内容,那么它们的执行顺序将直接影响最终的结果。 **3.2 解决方案** 为了解决命令间的协作问题,可以通过使用Elisp提供的高级特性来实现命令之间的同步。例如,可以...

Global site tag (gtag.js) - Google Analytics