thrift是通用型的,可以完成多种编程语言之间的交互,erl_nif是专用型的,主要完成Erlang和C语言之间的交互。thrift可以完成远程节点之间的交互,但是erl_nif只能完成本地的交互。
正因为thrift完成的是远程节点之间的交互,(thrift在facebook设计指出,主要的目的就是完成facebook数据中心中不同编程语言之间的交互)它在处理本地交互时,同样会看作是远程的方式进行工作;erl_nif的运行中,省去了远程节点的通信。所以,thrift的运行性能不如erl_nif的运行性能。
但是,如果要完成远程节点之间的语言交互,使用erl_nif,也不见得有多大优势。首先,远程之间之间的交互,必然要使用通信,就算是用原生的socket进行通信,性能上面而言,优势也甚微。但是,如果使用这种方式,开发效率就大大的降低了。
所以,如果在本地要完成C语言和Erlang语言之间的交互的话,使用erl_nif是没有错的(使用指南,可以google一下),但是如果要完成远程节点之间多编程语言的交互,选择thrift在一定程度上,还是要比erl_nif好。
贴出thrift完成CPP+Erlang计算100W以内prime的代码
CPP端的代码
#include <stdio.h>
#include <iostream>
#include <vector>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "../gen-cpp/Sendinfo.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace cpp2erl;
using namespace shared;
using namespace boost;
bool isPrime(int i)
{
int j;
int t = sqrt(i) + 1;
for(j = 2; j <= t; ++j)
{
if(i % j == 0)
return false;
}
return true;
}
int main(int argc, char** argv)
{
shared_ptr<TTransport> socket(new TSocket("localhost", 9909));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
SendinfoClient client(protocol);
timeval end,start;
try {
gettimeofday(&start,NULL);
vector<int> Mylist;
for(int i=0;i<1000000;i++)
{
if(isPrime(i))
{
Mylist.push_back(i);
}
}
gettimeofday(&end,NULL);
transport->open();
client.sendinfo(Mylist);
long timeuse = 1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec;
cout<<timeuse<<endl;
transport->close();
}
catch (TException &tx) {
printf("ERROR: %s\n", tx.what());
}
}
Erlang端的代码
-module(server).
-include("sendinfo_thrift.hrl").
-export([start/0,start/1,handle_function/2,stop/1,ping/0,zip/0,debug/2,sendinfo/1]).
debug(Format, Data) ->
error_logger:info_msg(Format, Data).
ping() ->
debug("ping()",[]),
ok.
sendinfo(Mylist) ->
{Nodename} = {Mylist},
debug("~w~n", [Nodename]).
zip() ->
debug("zip", []),
ok.
%%
start() ->
start(9909).
start(Port) ->
Handler = ?MODULE,
thrift_socket_server:start([{handler, Handler},
{service, sendinfo_thrift},
{port, Port},
{name, tutorial_server}]).
stop(Server) ->
thrift_socket_server:stop(Server).
handle_function(Function, Args) when is_atom(Function), is_tuple(Args) ->
case apply(?MODULE, Function, tuple_to_list(Args)) of
ok -> ok;
Reply -> {reply, Reply}
end.
测试:
1、测试thrift的额外消耗,修改CPP代码
gettimeofday(&start,NULL);
transport->open();
transport->close();
gettimeofday(&end,NULL);
long timeuse = 1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec;
cout<<timeuse<<endl;
transport open动作和close动作,需要消耗的时间在2462微秒左右(测试十八个数据,取其平均值)
可以看出,因使用thrift而产生的额外消耗,只有0.2462毫秒,与使用thrift带来的开发效率的提高,可以忽略不计。
2、thrift传输产生的消耗,同样需要修改下CPP代码
gettimeofday(&start,NULL);
client.sendinfo(Mylist);
gettimeofday(&end,NULL);
long timeuse = 1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec;
cout<<timeuse<<endl;
测试thrift完成传输需要消耗的时间,1545771微秒,也就是1.5秒左右
3、测试整个过程需要的时间
测试从判断prime到传给erlang 所需要的时间,1876668微秒,1900毫秒左右,和erl_nif完成该工作,相差了1秒左右,(参见:http://www.iteye.com/topic/534368)
总结:
虽然,thrift和erl_nif运行效率相比,确实不行,但是,thrift完成的多语言交互,可以轻松部署到远程节点,不仅如此,开发效率还要比erl_nif稍胜一筹(个人观点)。
君子性非异也,善假于物也。
分享到:
相关推荐
Thrift0.9.1 示例代码 Java版本 Thrift0.9.1 示例代码 Java版本
资源来自pypi官网。 资源全名:thrift_amqp_tornado-0.0.2.tar.gz
A known thrift source code
thrift安装包以及JAVA的一个测试项目
资源分类:Python库 所属语言:Python 资源全名:ctec_thrift_client_py3-1.0.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
spark-hive_2.11-2.3.0...spark-hive-thriftserver_2.11-2.3.0.jar log4j-2.15.0.jar slf4j-api-1.7.7.jar slf4j-log4j12-1.7.25.jar curator-client-2.4.0.jar curator-framework-2.4.0.jar curator-recipes-2.4.0.jar
本例是一个简单的thrift测试程序,包括客户端和服务端。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。
spark-hive-thriftserver_2.11-2.1.spark-hive-thrift
jacop.zip,java约束编程求解器(fork)java约束编程求解器。镜像https://git.iem.at/sciss/jacop
thrift_sasl-0.3.0.tar.gz
hbase_hive_thrift_php
Thrift Erlang软件库 执照 根据一项或多项贡献者许可协议获得了Apache Software Foundation(ASF)的许可。 有关版权拥有权的其他信息,请参见随此作品一起分发的NOTICE文件。... 1 > { ok , C0 } = thrift_client_uti
spark和hive结合依赖,如何使用请看我博客https://blog.csdn.net/z1987865446/article/details/109372818
Thrift-我的第二个烧瓶项目-是一个家庭财务管理应用程序。 它当前托管在-virajprateek.pythonanywhere.com。它是为我的个人用途定制的。
离线安装包,亲测可用
MiniMIRC使用Apache Thrift与RPC进行IRC聊天应用程序使用NetBeans应用程序生成NetBeans MiniIRC项目文件。 此应用程序使用MongoDB,因此必须事先在计算机上安装MongoDB进行测试。 安装MongoDB之后,运行程序的方式是...
python3通过thrift连接hbase模块修改版,其中hbase-python3,里面的path,根据实际实际路径修改.
python3连接hbase1.2.x所需的hbase连接库基于python3语法的修改版
php thrift 客户端调用 python服务端,学习了解thrif非常有用附代码和centos thift 安装文件