计算两路径的相对路径

9 篇文章 0 订阅
6 篇文章 0 订阅

需求:给定绝对路径A和绝对路径B,计算B相对于A的路径,例如A=/srv/test/   ,  B=/srv/foo/bar ,则结果为 ../foo/bar 。

思路:把A和B中具有相同的前面部分全部去掉,A中剩下的部分,有几个目录就替换为几个 ../ ,再把结果拼上 B的剩余部分就是结果。

从PHP手册看到有人写的一个函数:

<?php
 function relativePath($from, $to, $ps = DIRECTORY_SEPARATOR)
 {
   $arFrom = explode($ps, rtrim($from, $ps));
   $arTo = explode($ps, rtrim($to, $ps));
   while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
   {
     array_shift($arFrom);
     array_shift($arTo);
   }
   return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
 }
 ?>
假定参数分别为 from=/srv/test/   ,  to=/srv/foo/bar,上面的函数运行 10000次时间约为 0.1s

自己写个C版本的函数呢?看看效率如何?

#include <stdio.h>
#include <stdlib.h>
#define RESULT_LENGTH 100;
void relative_path(char *from,char *to, char *result){
        int i,j,k;
        i = j = 0;
        while(from[i] != '\0' && to[i] != '\0' && from[i] == to[i]){
                i++;
        }
        k = i;
        while(from[k] != '\0'){
                if(from[k++] == '/'){
                        result[j++]='.';
                        result[j++]='.';
                        result[j++]='/';
                }else{
                        continue;
                }
        }
        while(to[i] != '\0'){
                result[j++] = to[i++];
        }
        result[j] = '\0';
}
int main(int argc, char *argv[]){
        char *from = argv[1];
        char *to = argv[2];
        char *result;
        result = (char *)malloc(sizeof(char)*RESULT_LENGTH);
        int i;
        for(i=0;i<10000000;i++){
                relative_path(from,to,result);
        }
        printf("%s\n",result);
        free(result);
        result = NULL;
        return 0;
}
假定参数分别为 from=/srv/test/   ,  to=/srv/foo/bar,上面的函数运行 1000万次时间约为 0.4s

好吧,对比一下,差距接近250倍





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值