zoukankan      html  css  js  c++  java
  • Node.js和PHP运行机制对比

    为什么要用node.js它又有什么优势呢?一个新的技术被大家喜爱那么它就必然有它的优势,那么下面我们就来简单把它和php做一个对比

    1 . Node.js 他用的是JavaScript引擎,那么注定它是单线程 ,使用异步方法开辟多个任务,无需像php等待上个任务线程使用结束之后给下个使用,

       PHP也是单线程但是它借用Apache服务器提供多线程服务

    2 . 高并发,大数据量怎么处理:
    php : 优化sql ,用组件,用缓存,为了让线程尽快结束,进行下一次任务
    node:单线程 、异步、事件驱动
     
    下面是他们处理事件,
         php上下衔接依次执行,
         node中因为运行速度很快并不会等待,所以如果后面用到前面返回的结果,就需要把后面的封装起来,作为一个回调函数执行
    node.js vs php
     
    优点:
      性能高(运行机制问题)
      开发效率高(省不少优化的事)
      应用范围广(可以开发桌面系统,electron框架)
    缺点:
      新、人少
      中间件少
      IDE不完善
      
    node.js的劣势和解决方案
      
      1 默认不支持多核,但可以用cluster解决
      2 默认不支持服务器集群,node-http-proxy可以解决
      3 使用nginx做负载均衡,静态的由nginx处理,动态的有node.js处理
      4 forever或node-cluster实现灾难恢复
     
     
    下面是 一个数据测试
      请求-->随机创建内容是20k字符的文档 -->读取文档 -->输出
      结果是 node所需的时间要远小于php所用的时间
    1 Node
      
    var  http  =  require ( 'http' ) ;    
    
     http.createServer ( function  handler ( req ,  res )   {
        res.writeHead ( 200 , {'Content-Type' : 'text/html ; charset=utf-8' });
        if (req.url !== '/favicon.ico') {
    
            str = "" ;   //随机字符 - 20k
            //随机生成文件
            fileName = String.fromCharCode ( Math. floor ( 65 + ( Math. random () * ( 122 - 65 )))) + ".txt" ;
            //str 赋值
             for ( i = 0; i < 200000; i++ ){
    
                n = Math. floor ( 65 + ( Math. random () * ( 122 - 65 )) ) ;
                str += String. fromCharCode ( n ) ;
    
             }
    
            //写入
            var fs = require ( 'fs' ) ;//操作文件模块
            //写入内容
            fs.writeFile ( fileName,str,function ( err, fd )   {
    
                     if ( err ) throw  err ; //如果错误则抛出错误
    
                     //读取文件 并展示的页面
                    fs.readFile ( fileName , function( err, data ){
    
                        if   ( err )   throw  err ; 
    
                        res.write(data);//输出
                        res.end ('') ; // 结束
    
                     }) ;   
                 }
             );
        }
    }).listen(8000) ;
    
    console. log ( 'success:8000' ) ;

    PHP

      

     1 <?php
     2 
     3 $str = "" ;   //随机字符串
     4 // 文本名字
     5 $fileName   = chr ( rand ( 0 , 57 ) + 65 ).'.txt' ;
     6 
     7 for ( $i = 0 ; $i < 200000 ; $i ++ ){
     8 
     9      $n = rand ( 0 , 57 ) + 65 ;
    10      $str   =   $str . chr ( $n ) ;
    11 }
    12 
    13 //写入
    14 
    15 file_put_contents( $fileName , $str ) ;
    16 
    17 $result = file_get_contents ( $fileName ) ;
    18 
    19 echo $result ;
    20 ?>
     
  • 相关阅读:
    'Undefined symbols for architecture i386,clang: error: linker command failed with exit code 1
    The codesign tool requires there only be one 解决办法
    XCode iOS project only shows “My Mac 64bit” but not simulator or device
    Provisioning profile XXXX can't be found 的解决办法
    UIView 中的控件事件穿透 Passthrough 的实现
    Xcode4.5出现时的OC新语法
    xcode 快捷键(持续更新)
    打越狱包
    php缓存与加速分析与汇总
    浏览器的判断
  • 原文地址:https://www.cnblogs.com/NTWang/p/6271696.html
Copyright © 2011-2022 走看看